Related
I am having trouble creating this view on SQL. As a query, it runs fine, but when trying to save it as a view, it gives me the "the multi-part identifier could not be bound" error. I think it has something to do with implicit joins.
The issue occurs when inserting the part with the extra tabs in the script below (case statements).
Is there a way to rewrite it to save it as a view?
Thanks
WITH MNP AS
(SELECT
GoodsReceivedID AS ID,
PORef,
MIN(IncomingDate) AS Manip_Start_Date,
SUM(CASE WHEN ArticleType = 'TEX' THEN UNITS ELSE 0 END) AS TEX_Mnp,
SUM(CASE WHEN ArticleType = 'ACC' THEN UNITS ELSE 0 END) AS ACC_Mnp,
SUM(CASE WHEN ArticleType = 'SHOE' THEN UNITS ELSE 0 END) AS SHOE_Mnp,
SUM(CASE WHEN ArticleType = 'TARA' THEN UNITS ELSE 0 END) AS TARA_Mnp,
SUM(CASE WHEN ArticleType NOT IN ('TEX','ACC','SHOE','TARA') THEN UNITS ELSE 0 END) AS MIX_Mnp,
SUM(UNITS) AS Total_Units
FROM dbo.NT_ManipulationResult
GROUP BY PORef, GoodsReceivedID),
INV AS
(SELECT
GoodsReceivedID,
SUM(CASE WHEN UNITOFSUPPLYID = 1 THEN IN_TOTAL_UNITS END) AS Invoice_Units,
SUM (CASE WHEN UNITOFSUPPLYID = 2 THEN IN_TOTAL_UNITS END) AS Invoice_KG,
SUM (InvoiceNet + InvoiceVAT) AS Invoice_TOT
FROM [dbo].[NT_GoodsReceivedInvoice]
GROUP BY GoodsReceivedID
)
SELECT
GR.ID,
GR.VL_Ref,
GR.DateReceived,
MNP.Manip_Start_Date,
COALESCE(DATEDIFF (Day, DateReceived, MNP.Manip_Start_Date),'') AS DaysToManipulate,
WR.Name AS Warehouse,
GR.ArticleOriginID AS Origin,
SP.Name AS Supplier,
GD.Name AS Grade,
COALESCE(INV.Invoice_TOT,0) AS Invoice_TOT,
COALESCE(INV.Invoice_Units,0) AS Invoice_Units,
COALESCE(INV.Invoice_KG,0) AS Invoice_KG,
COALESCE(MNP.Total_Units,0) AS ManipulatedUnits,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN COALESCE(MNP.Total_Units,0) - COALESCE(INV.Invoice_Units,0) ELSE 0 END AS Manip_Difference,
COALESCE(MNP.TEX_Mnp,0) AS TEX_Mnp,
COALESCE(MNP.ACC_Mnp,0) AS ACC_Mnp,
COALESCE(MNP.SHOE_Mnp,0) AS SHOE_Mnp,
COALESCE(MNP.TARA_Mnp,0) AS TARA_Mnp,
COALESCE(MNP.MIX_Mnp,0) AS MIX_Mnp,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN (COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS TEX_Cost,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN 0.35*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS ACC_Cost,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN 1.2*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS SHOE_Cost,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN 0.45*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS TARA_Cost,
CASE WHEN COALESCE(MNP.Total_Units,0) <> 0 THEN 0.8*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS MIX_Cost
FROM dbo.NT_GoodsReceived AS GR
LEFT OUTER JOIN MNP ON GR.ID = MNP.ID
LEFT OUTER JOIN [dbo].[NT_Warehouse] WR ON GR.WarehouseID = WR.ID
LEFT OUTER JOIN [dbo].[NT_ArticleOrigin] AO ON GR.ArticleOriginID = AO.ID
LEFT OUTER JOIN [dbo].[NT_Supplier] SP ON GR.SupplierID = SP.ID
LEFT OUTER JOIN [dbo].[NT_ArticleGrade] GD ON GR.ArticleGradeID = GD.ID
LEFT OUTER JOIN INV ON GR.ID = INV.GoodsReceivedID
WHERE GR.ID NOT IN (19,20)
I found a solution:
Instead of adding MNP and INV as WITH statements within the query, I created them as separate views which I referenced to within this new query which I could now save as view.
Script ended as follows:
SELECT
GR.ID,
GR.VL_Ref,
GR.DateReceived,
MNP.Manip_Start_Date,
COALESCE (DATEDIFF (Day, GR.DateReceived, MNP.Manip_Start_Date),'') AS DaysToManipulate,
WR.Name AS Warehouse,
GR.ArticleOriginID AS Origin,
SP.Name AS Supplier,
GD.Name AS Grade,
COALESCE(INV.Invoice_TOT,0) AS Invoice_TOT,
COALESCE(INV.Invoice_Units,0) AS Invoice_Units,
COALESCE(INV.Invoice_KG,0) AS Invoice_KG,
COALESCE(MNP.Total_Units,0) AS ManipulatedUnits,
CASE WHEN MNP.Total_Units IS NOT NULL THEN MNP.Total_Units - INV.Invoice_Units ELSE 0 END AS Manip_Difference,
COALESCE(MNP.TEX_Mnp,0) AS TEX_Mnp,
COALESCE(MNP.ACC_Mnp,0) AS ACC_Mnp,
COALESCE(MNP.SHOE_Mnp,0) AS SHOE_Mnp,
COALESCE(MNP.TARA_Mnp,0) AS TARA_Mnp,
COALESCE(MNP.MIX_Mnp,0) AS MIX_Mnp,
CASE WHEN MNP.Total_Units IS NOT NULL THEN (COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS TEX_Cost,
CASE WHEN MNP.Total_Units IS NOT NULL THEN 0.35*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS ACC_Cost,
CASE WHEN MNP.Total_Units IS NOT NULL THEN 1.2*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS SHOE_Cost,
CASE WHEN MNP.Total_Units IS NOT NULL THEN 0.45*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS TARA_Cost,
CASE WHEN MNP.Total_Units IS NOT NULL THEN 0.8*(COALESCE(INV.Invoice_TOT,0) / (COALESCE(MNP.TEX_Mnp,0) + 0.35*COALESCE(MNP.ACC_Mnp,0) + 1.2*COALESCE(MNP.SHOE_Mnp,0) + .45*COALESCE(MNP.TARA_Mnp,0) +.8*COALESCE(MNP.MIX_Mnp,0))) ELSE 0 END AS MIX_Cost
FROM dbo.NT_GoodsReceived AS GR
LEFT OUTER JOIN MNP ON GR.ID = MNP.ID
LEFT OUTER JOIN dbo.NT_Warehouse AS WR ON GR.WarehouseID = WR.ID
LEFT OUTER JOIN dbo.NT_ArticleOrigin AS AO ON GR.ArticleOriginID = AO.ID
LEFT OUTER JOIN dbo.NT_Supplier AS SP ON GR.SupplierID = SP.ID
LEFT OUTER JOIN dbo.NT_ArticleGrade AS GD ON GR.ArticleGradeID = GD.ID
LEFT OUTER JOIN INV ON GR.ID = INV.GoodsReceivedID
WHERE (GR.ID NOT IN (19, 20))
I have taken over the support of an Excel 2010 macro and need to change the sort order of a recordset.
The query below worked before I attempted to change the order by adding rl.list_order.
Now I'm getting the error "The multi-part identifier could not be bound" and have tried many alternatives and have researched on this site for a solution but haven't been able to solve this. Can someone please help.
SELECT ips_rc.project, lfbe.Category, ips_rc.roster_id, ips_rc.role_category, ips_rc.role_title AS Role, ips_rc.role_code, ips_rc.role_title + ' (' + ips_rc.role_code + ')' AS PS_Role, CASE WHEN ISNULL(lfbe.Pronto_Role_Code, '') = '' THEN 'TO_BE_MAPPED' ELSE ISNULL(lfbe.Pronto_Role_Code, '') END AS Pronto_Role_Code, CASE WHEN ISNULL(lfbe.Pronto_Role_Code_Description, '') = '' THEN ips_rc.role_title ELSE ISNULL(lfbe.Pronto_Role_Code_Description, '') END AS Pronto_Role_Code_Description, ISNULL(lfbe.Rate, 0) AS Rate, CASE WHEN ISNULL(lfbe.Pronto_Role_Code_Old, '') = '' THEN ips_rc.role_title ELSE lfbe.Pronto_Role_Code_Old END AS Pronto_Role_Code_Old, CASE WHEN ISNULL(lfbe.Pronto_Role_Code_Description_Old, '') = '' THEN 'TO_BE_MAPPED' ELSE lfbe.Pronto_Role_Code_Description_Old END AS Pronto_Role_Code_Description_Old, ips_rc.Shift, ips_rc.UOM, ips_rc.Date, ips_rc.DHours, ips_rc.NHours FROM (SELECT psrc.project, pr_jcm.customer, pr_jcm.subcode, psrc.roster_id, psrc.role_id, psrc.role_category, psrc.role_title,
psrc.role_code, CASE WHEN SUM(CASE WHEN psrc.Date BETWEEN psrc.start_date AND psrc.end_date THEN psrc.NWorkHours ELSE 0 END) OVER (PARTITION BY psrc.project, psrc.roster_id) >= SUM(CASE WHEN psrc.Date BETWEEN psrc.start_date AND psrc.end_date THEN psrc.DWorkHours ELSE 0 END) OVER (PARTITION BY psrc.project, psrc.roster_id) THEN 'Night Shifts' ELSE 'Day Shifts' END AS Shift, 'HOURS' AS UOM, psrc.Date, psrc.DHours, psrc.NHours FROM (SELECT rcd.project, rcd.start_date, rcd.end_date, rcd.roster_id, rcd.role1_id AS role_id, CASE WHEN rb_test1.role1_id IS NOT NULL THEN rb_test1.category WHEN rb_test2.role1_id IS NOT NULL THEN rb_test2.category WHEN rb_test3.role1_id IS NOT NULL THEN rb_test3.category ELSE rcd.role_category END AS role_category, CASE WHEN rb_test1.role1_id IS NOT NULL THEN rb_test1.title WHEN rb_test2.role1_id IS NOT NULL THEN rb_test2.title WHEN rb_test3.role1_id IS NOT NULL THEN rb_test3.title ELSE rcd.role_title END AS role_title, CASE WHEN rb_test1.role1_id IS NOT NULL THEN CAST(rb_test1.band_r
ole_id AS VARCHAR) WHEN rb_test2.role1_id IS NOT NULL THEN CAST(rb_test2.band_role_id AS VARCHAR) WHEN rb_test3.role1_id IS NOT NULL THEN CAST(rb_test3.band_role_id AS VARCHAR) ELSE rcd.role_code END AS role_code, rcd.shift_date AS Date, SUM(CASE WHEN rcd.shift_ampm = 'day' THEN rcd.shift_time ELSE 0 END) AS DHours, SUM(CASE WHEN rcd.shift_ampm = 'day' AND rcd.shift_type = 'work' THEN rcd.shift_time ELSE 0 END) AS DWorkHours, SUM(CASE WHEN rcd.shift_ampm = 'day' AND rcd.shift_type = 'eqmob' THEN rcd.shift_time ELSE 0 END) AS DEqmobHours, SUM(CASE WHEN rcd.shift_ampm = 'day' AND rcd.shift_type = 'eqdemob' THEN rcd.shift_time ELSE 0 END) AS DEqdemobHours, SUM(CASE WHEN rcd.shift_ampm = 'night' THEN rcd.shift_time ELSE 0 END) AS NHours, SUM(CASE WHEN rcd.shift_ampm = 'night' AND rcd.shift_type = 'work' THEN rcd.shift_time ELSE 0 END) AS NWorkHours, SUM(CASE WHEN rcd.shift_ampm = 'night' AND rcd.shift_type = 'eqmob' THEN rcd.shift_time ELSE 0 END) AS NEqmobHours, SUM(CASE WHEN rcd.shift_ampm = 'night' AND rcd.sh
ift_type = 'eqdemob' THEN rcd.shift_time ELSE 0 END) AS NEqdemobHours FROM OPENQUERY([LFMANAGE], 'SELECT p.job_no AS project, p.start_date, p.end_date, rs.roster_id, IFNULL(rs.role_id, 0) AS role1_id, IFNULL(r.role2_id, 0) AS role2_id, IFNULL(r.role3_id, 0) AS role3_id, rl.category AS role_category, rl.title AS role_title, rl.role_code AS role_code, rs.shift_date, rs.shift_ampm, rs.shift_time, rs.shift_type, s.client_id FROM lfmanage.t_roster_shift rs JOIN lfmanage.t_project AS p ON p.project_id = rs.project_id LEFT JOIN lfmanage.t_site AS s ON s.site_id = p.site_id JOIN lfmanage.t_roster AS r ON r.roster_id = rs.roster_id JOIN lfmanage.t_role AS rl ON rl.role_id = rs.role_id WHERE p.job_no = ''700704'' AND CASE WHEN ISNULL(rs.deleted) THEN 0 ELSE rs.deleted END = 0') AS rcd LEFT OUTER JOIN (SELECT client_id, band_role_id, title, CASE WHEN CHARINDEX(UPPER(title),'SUPER') > 0 THEN 'super' ELSE 'trade' END AS category, role1_id, role2_id, role3_id FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_r
ole_id) AS band_role_id, br.title, brs.role1_id, brs.role2_id, brs.role3_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role2_id, 0) <> 0 AND IFNULL(brs.role3_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role2_id, brs.role3_id')) AS rb_test1 ON rb_test1.client_id = rcd.client_id AND rb_test1.role1_id = rcd.role1_id AND rb_test1.role2_id = rcd.role2_id AND rb_test1.role3_id = rcd.role3_id LEFT OUTER JOIN (SELECT client_id, band_role_id, title, CASE WHEN CHARINDEX(UPPER(title),'SUPER') > 0 THEN 'super' ELSE 'trade' END AS category, role1_id, role2_id FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_role_id) AS band_role_id, br.title, brs.role1_id, brs.role2_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role2_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role2_id '
)) AS rb_test2 ON rb_test2.client_id = rcd.client_id AND rb_test2.role1_id = rcd.role1_id AND rb_test2.role2_id = rcd.role2_id LEFT OUTER JOIN (SELECT client_id, band_role_id, title, CASE WHEN CHARINDEX(UPPER(title),'SUPER') > 0 THEN 'super' ELSE 'trade' END AS category, role1_id, role3_id FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_role_id) AS band_role_id, br.title, brs.role1_id, brs.role3_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role3_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role3_id ')) AS rb_test3 ON rb_test3.client_id = rcd.client_id AND rb_test3.role1_id = rcd.role1_id AND rb_test3.role3_id = rcd.role3_id WHERE rcd.shift_date > '2017-01-01' GROUP BY rcd.project, rcd.start_date, rcd.end_date, rcd.roster_id, rcd.role1_id, CASE WHEN rb_test1.role1_id IS NOT NULL THEN rb_test1.category WHEN rb_test2.role1_id IS NOT NULL THEN rb_test2.category WHEN rb_test3.role
1_id IS NOT NULL THEN rb_test3.category ELSE rcd.role_category END, CASE WHEN rb_test1.role1_id IS NOT NULL THEN rb_test1.title WHEN rb_test2.role1_id IS NOT NULL THEN rb_test2.title WHEN rb_test3.role1_id IS NOT NULL THEN rb_test3.title ELSE rcd.role_title END, CASE WHEN rb_test1.role1_id IS NOT NULL THEN CAST(rb_test1.band_role_id AS VARCHAR) WHEN rb_test2.role1_id IS NOT NULL THEN CAST(rb_test2.band_role_id AS VARCHAR) WHEN rb_test3.role1_id IS NOT NULL THEN CAST(rb_test3.band_role_id AS VARCHAR) ELSE rcd.role_code END, rcd.shift_date) AS psrc INNER JOIN (SELECT job_no AS project, Client AS customer, '3700002213' AS subcode FROM [dbo].[vw_PlanningSystem_Projects] WHERE job_no = '700704') AS pr_jcm ON pr_jcm.project = psrc.project) AS ips_rc LEFT OUTER JOIN (SELECT rates.Client, rates.SubCode, mapping.Category, mapping.Shift, mapping.IPS_Role_Code, rates.Pronto_Role_Code, rates.Pronto_Role_Code_Description, rates.Rate, rates.Pronto_Role_Code_Old, rates.Pronto_Role_Code_Description_Old FROM (SELECT UPPER(Ca
tegory) AS Category, 'Day Shifts' AS Shift, IPS_Role_Code, Day_Cost_Code As Pronto_Role_Code FROM [LFBudgetEstimate].[dbo].[LFBE_RoleCode_MasterMapping] UNION ALL SELECT UPPER(Category) AS Category, 'Night Shifts' AS Shift, IPS_Role_Code, Night_Cost_Code As Pronto_Role_Code FROM [LFBudgetEstimate].[dbo].[LFBE_RoleCode_MasterMapping]) AS mapping INNER JOIN (SELECT Client, SubCode, CASE WHEN Shift = 'Day Shift' THEN 'Day Shifts' WHEN Shift = 'Night Shift' THEN 'Night Shifts' Else '' END AS Shift, Pronto_Role_Code, Pronto_Role_Code_Description, Rate, Pronto_Role_Code_Old, Pronto_Role_Code_Description_Old FROM [LFBudgetEstimate].[dbo].[LFBE_SubCodes_Rates]) AS rates ON rates.Shift = mapping.Shift AND rates.Pronto_Role_Code = mapping.Pronto_Role_Code) AS lfbe ON lfbe.Client = ips_rc.customer AND lfbe.SubCode = ips_rc.subCode AND lfbe.Shift = ips_rc.Shift AND lfbe.IPS_Role_Code = ips_rc.role_code ORDER BY ips_rc.project ASC, ips_rc.role_title ASC, rl.list_order
Thanks for your help
Even with the formatting, you've got a lot going on here... Stack Overflow veterans, I'm just starting to dip my toes in here, so if this isn't correct, give me some grace.
In your first OPENQUERY statement, I don't see rl.list_order as a field being returned. If that's a column you're trying to ORDER BY, I think you should be able to add rl.list_order to your SELECT list within that statement which you alias as rcd.
Since that's within a nested sub-query, you'll want to elevate it to each of the calling queries. I think it's something like this... Try it out and post back with any errors.
SELECT ips_rc.ListOrder
, ips_rc.project
, lfbe.Category
, ips_rc.roster_id
, ips_rc.role_category
, ips_rc.role_title AS [ROLE]
, ips_rc.role_code
, ips_rc.role_title + ' (' + ips_rc.role_code + ')' AS PS_Role
, CASE
WHEN ISNULL(lfbe.Pronto_Role_Code, '') = ''
THEN 'TO_BE_MAPPED'
ELSE ISNULL(lfbe.Pronto_Role_Code, '')
END AS Pronto_Role_Code
, CASE
WHEN ISNULL(lfbe.Pronto_Role_Code_Description, '') = ''
THEN ips_rc.role_title
ELSE ISNULL(lfbe.Pronto_Role_Code_Description, '')
END AS Pronto_Role_Code_Description
, ISNULL(lfbe.Rate, 0) AS Rate
, CASE
WHEN ISNULL(lfbe.Pronto_Role_Code_Old, '') = ''
THEN ips_rc.role_title
ELSE lfbe.Pronto_Role_Code_Old
END AS Pronto_Role_Code_Old
, CASE
WHEN ISNULL(lfbe.Pronto_Role_Code_Description_Old, '') = ''
THEN 'TO_BE_MAPPED'
ELSE lfbe.Pronto_Role_Code_Description_Old
END AS Pronto_Role_Code_Description_Old
, ips_rc.Shift
, ips_rc.UOM
, ips_rc.DATE
, ips_rc.DHours
, ips_rc.NHours
FROM (
SELECT psrc.ListOrder -- Added
, psrc.project
, pr_jcm.customer
, pr_jcm.subcode
, psrc.roster_id
, psrc.role_id
, psrc.role_category
, psrc.role_title
, psrc.role_code
, CASE
WHEN SUM(CASE
WHEN psrc.DATE BETWEEN psrc.start_date
AND psrc.end_date
THEN psrc.NWorkHours
ELSE 0
END) OVER (PARTITION BY psrc.project, psrc.roster_id) >= SUM(CASE
WHEN psrc.DATE BETWEEN psrc.start_date
AND psrc.end_date
THEN psrc.DWorkHours
ELSE 0
END) OVER (PARTITION BY psrc.project, psrc.roster_id)
THEN 'Night Shifts'
ELSE 'Day Shifts'
END AS Shift, 'HOURS' AS UOM, psrc.DATE, psrc.DHours, psrc.NHours
FROM (
SELECT rcd.list_order --Added
, rcd.project
, rcd.start_date
, rcd.end_date
, rcd.roster_id
, rcd.role1_id AS role_id
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN rb_test1.category
WHEN rb_test2.role1_id IS NOT NULL
THEN rb_test2.category
WHEN rb_test3.role1_id IS NOT NULL
THEN rb_test3.category
ELSE rcd.role_category
END AS role_category
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN rb_test1.title
WHEN rb_test2.role1_id IS NOT NULL
THEN rb_test2.title
WHEN rb_test3.role1_id IS NOT NULL
THEN rb_test3.title
ELSE rcd.role_title
END AS role_title
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN CAST(rb_test1.band_role_id AS VARCHAR)
WHEN rb_test2.role1_id IS NOT NULL
THEN CAST(rb_test2.band_role_id AS VARCHAR)
WHEN rb_test3.role1_id IS NOT NULL
THEN CAST(rb_test3.band_role_id AS VARCHAR)
ELSE rcd.role_code
END AS role_code, rcd.shift_date AS DATE
, SUM(CASE
WHEN rcd.shift_ampm = 'day'
THEN rcd.shift_time
ELSE 0
END) AS DHours
, SUM(CASE
WHEN rcd.shift_ampm = 'day'
AND rcd.shift_type = 'work'
THEN rcd.shift_time
ELSE 0
END) AS DWorkHours
, SUM(CASE
WHEN rcd.shift_ampm = 'day'
AND rcd.shift_type = 'eqmob'
THEN rcd.shift_time
ELSE 0
END) AS DEqmobHours
, SUM(CASE
WHEN rcd.shift_ampm = 'day'
AND rcd.shift_type = 'eqdemob'
THEN rcd.shift_time
ELSE 0
END) AS DEqdemobHours
, SUM(CASE
WHEN rcd.shift_ampm = 'night'
THEN rcd.shift_time
ELSE 0
END) AS NHours
, SUM(CASE
WHEN rcd.shift_ampm = 'night'
AND rcd.shift_type = 'work'
THEN rcd.shift_time
ELSE 0
END) AS NWorkHours
, SUM(CASE
WHEN rcd.shift_ampm = 'night'
AND rcd.shift_type = 'eqmob'
THEN rcd.shift_time
ELSE 0
END) AS NEqmobHours
, SUM(CASE
WHEN rcd.shift_ampm = 'night'
AND rcd.shift_type = 'eqdemob'
THEN rcd.shift_time
ELSE 0
END) AS NEqdemobHours
FROM OPENQUERY([LFMANAGE], 'SELECT p.job_no AS project, p.start_date, p.end_date, rs.roster_id, IFNULL(rs.role_id, 0) AS role1_id, IFNULL(r.role2_id, 0) AS role2_id, IFNULL(r.role3_id, 0) AS role3_id, rl,ListOrder, rl.category AS role_category, rl.title AS role_title, rl.role_code AS role_code, rs.shift_date, rs.shift_ampm, rs.shift_time, rs.shift_type, s.client_id FROM lfmanage.t_roster_shift rs JOIN lfmanage.t_project AS p ON p.project_id = rs.project_id LEFT JOIN lfmanage.t_site AS s ON s.site_id = p.site_id JOIN lfmanage.t_roster AS r ON r.roster_id = rs.roster_id JOIN lfmanage.t_role AS rl ON rl.role_id = rs.role_id WHERE p.job_no = ''700704'' AND CASE WHEN ISNULL(rs.deleted) THEN 0 ELSE rs.deleted END = 0') AS rcd --Added
LEFT JOIN (
SELECT client_id
, band_role_id
, title
, CASE
WHEN CHARINDEX(UPPER(title), 'SUPER') > 0
THEN 'super'
ELSE 'trade'
END AS category
, role1_id
, role2_id
, role3_id
FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_role_id) AS band_role_id, br.title, brs.role1_id, brs.role2_id, brs.role3_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role2_id, 0) <> 0 AND IFNULL(brs.role3_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role2_id, brs.role3_id')
) AS rb_test1
ON rb_test1.client_id = rcd.client_id
AND rb_test1.role1_id = rcd.role1_id
AND rb_test1.role2_id = rcd.role2_id
AND rb_test1.role3_id = rcd.role3_id
LEFT JOIN (
SELECT client_id, band_role_id, title, CASE
WHEN CHARINDEX(UPPER(title), 'SUPER') > 0
THEN 'super'
ELSE 'trade'
END AS category, role1_id, role2_id
FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_role_id) AS band_role_id, br.title, brs.role1_id, brs.role2_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role2_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role2_id ')
) AS rb_test2
ON rb_test2.client_id = rcd.client_id
AND rb_test2.role1_id = rcd.role1_id
AND rb_test2.role2_id = rcd.role2_id
LEFT JOIN (
SELECT client_id, band_role_id, title, CASE
WHEN CHARINDEX(UPPER(title), 'SUPER') > 0
THEN 'super'
ELSE 'trade'
END AS category, role1_id, role3_id
FROM OPENQUERY([LFMANAGE], 'SELECT br.client_id, MIN(brs.band_role_id) AS band_role_id, br.title, brs.role1_id, brs.role3_id FROM t_band_role_set AS brs INNER JOIN t_band_role AS br ON br.band_role_id = brs.band_role_id WHERE IFNULL(brs.role1_id, 0) <> 0 AND IFNULL(brs.role3_id, 0) <> 0 GROUP BY br.client_id, br.title, brs.role1_id, brs.role3_id ')
) AS rb_test3
ON rb_test3.client_id = rcd.client_id
AND rb_test3.role1_id = rcd.role1_id
AND rb_test3.role3_id = rcd.role3_id
WHERE rcd.shift_date > '2017-01-01'
GROUP BY rcd.list_order --Added
, rcd.project
, rcd.start_date
, rcd.end_date
, rcd.roster_id
, rcd.role1_id
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN rb_test1.category
WHEN rb_test2.role1_id IS NOT NULL
THEN rb_test2.category
WHEN rb_test3.ROLE1_id IS NOT NULL
THEN rb_test3.category
ELSE rcd.role_category
END
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN rb_test1.title
WHEN rb_test2.role1_id IS NOT NULL
THEN rb_test2.title
WHEN rb_test3.role1_id IS NOT NULL
THEN rb_test3.title
ELSE rcd.role_title
END
, CASE
WHEN rb_test1.role1_id IS NOT NULL
THEN CAST(rb_test1.band_role_id AS VARCHAR)
WHEN rb_test2.role1_id IS NOT NULL
THEN CAST(rb_test2.band_role_id AS VARCHAR)
WHEN rb_test3.role1_id IS NOT NULL
THEN CAST(rb_test3.band_role_id AS VARCHAR)
ELSE rcd.role_code
END
, rcd.shift_date
) AS psrc
INNER JOIN (
SELECT job_no AS project
, Client AS customer
, '3700002213' AS subcode
FROM [dbo].[vw_PlanningSystem_Projects]
WHERE job_no = '700704'
) AS pr_jcm
ON pr_jcm.project = psrc.project
) AS ips_rc
LEFT JOIN (
SELECT rates.Client
, rates.SubCode
, mapping.Category
, mapping.Shift
, mapping.IPS_Role_Code
, rates.Pronto_Role_Code
, rates.Pronto_Role_Code_Description
, rates.Rate
, rates.Pronto_Role_Code_Old
, rates.Pronto_Role_Code_Description_Old
FROM (
SELECT UPPER(Category) AS Category
, 'Day Shifts' AS Shift
, IPS_Role_Code
, Day_Cost_Code AS Pronto_Role_Code
FROM [LFBudgetEstimate].[dbo].[LFBE_RoleCode_MasterMapping]
UNION ALL
SELECT UPPER(Category) AS Category
, 'Night Shifts' AS Shift
, IPS_Role_Code
, Night_Cost_Code AS Pronto_Role_Code
FROM [LFBudgetEstimate].[dbo].[LFBE_RoleCode_MasterMapping]
) AS mapping
INNER JOIN (
SELECT Client
, SubCode
, CASE
WHEN Shift = 'Day Shift'
THEN 'Day Shifts'
WHEN Shift = 'Night Shift'
THEN 'Night Shifts'
ELSE ''
END AS Shift
, Pronto_Role_Code
, Pronto_Role_Code_Description
, Rate
, Pronto_Role_Code_Old
, Pronto_Role_Code_Description_Old
FROM [LFBudgetEstimate].[dbo].[LFBE_SubCodes_Rates]
) AS rates
ON rates.Shift = mapping.Shift
AND rates.Pronto_Role_Code = mapping.Pronto_Role_Code
) AS lfbe
ON lfbe.Client = ips_rc.customer
AND lfbe.SubCode = ips_rc.subCode
AND lfbe.Shift = ips_rc.Shift
AND lfbe.IPS_Role_Code = ips_rc.role_code
ORDER BY ips_rc.project ASC, ips_rc.role_title ASC, ips_rc.list_order ASC
You're right CSharp821 there's an awful lot going on there. I made the changes you suggested and it now works!
You are trying to access alias rl, however the rl alias is only visible within the OPENQUERY which has an alias of rcd. Try order by rcd.list_order instead of rl.list_order
Weird thing happening.
Query below runs less than a second, but if I change anything(like extra space) in WHERE clause then the whole query running 7 seconds.
Or if I comment out one of those lines (SELECT ISNULL(SUM(...) ...
Never see that before...
SELECT...
...
FROM ...
--If I put extra space after SELECT - the whole query running 7 seconds
WHERE EXISTS
(SELECT * FROM tblClaims_ReservePayments RP
WHERE
CASE WHEN #coverageTypeId IS NULL THEN 1
ELSE
CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1
AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND RP.ClaimId = C.ClaimId)
AND
CASE WHEN #companyGuid IS NULL THEN 1 ELSE CASE WHEN CPI.CompanyGuid = #companyGuid THEN 1 ELSE 0 END END = 1
AND
EXISTS (SELECT * FROM tblClaims_Claimants CLMNT WHERE CLMNT.ClaimID = C.ClaimID AND CLMNT.StatusId = 0)
AND EXISTS (SELECT * FROM #Adjusters WHERE adjuster = C.InhouseAdjuster)
ORDER BY
CPI.PolicyNumber
What is going on?
Is it parameter sniffing?
The whole statement is below:
DECLARE
#dateFrom DATETIME = '2017-02-01',
#dateTo DATETIME = '2017-05-31',
#companyGuid UNIQUEIDENTIFIER = 'A95645F7-74CF-4551-BD28-5CBC5AD77EF2',-- NULL, Philadelphia
#coverageTypeId INT = NULL,
#inhouseAdjuster varchar(8000) = NULL
DECLARE #Adjusters TABLE(adjuster uniqueidentifier)
IF(#inhouseAdjuster IS NOT NULL AND #inhouseAdjuster <> '')
BEGIN
INSERT INTO #Adjusters
SELECT Guid FROM dbo.StringOfGuidsToTable(#inhouseAdjuster, ',')
END
ELSE
BEGIN
INSERT INTO #Adjusters
SELECT InhouseAdjuster FROM tblClaims_Claim
END
DECLARE #AdjusterString varchar(500)
SET #AdjusterString = ''
DECLARE #CoveringTable TABLE (Covering varchar(1000))
INSERT INTO #CoveringTable
SELECT DISTINCT tblUsers.FirstName + ' ' + tblUsers.LastName FROM tblClaims_Claim
inner join tblUsers on tblUsers.userguid = tblClaims_Claim.userguid
WHERE tblClaims_Claim.InhouseAdjuster IN (SELECT adjuster FROM #Adjusters)
SELECT #AdjusterString = #AdjusterString + CASE WHEN #AdjusterString = '' THEN '' ELSE ', ' END + Covering FROM #CoveringTable
SELECT
CPI.PolicyNumber,
CC.DateReported,--------------------------------------added by Oleg 5/1/2017
C.LossDate as ForfeitureDate,
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription <> 'Expense' WHERE RP.ClaimId = C.ClaimId AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [BondAmount],
dbo.GetEntityName(CPI.InsuredGuid) as Defendant,
CC.UserDef_ClaimantId as CaseNumber,
CC.CorporationName, -----------------------------------------------------------------------------------------------------------------------added by Oleg 5/1/2017
PRIM_ADDRESS.City,-----------------------------------------------------------------------------------------------------------------------added by Oleg 5/1/2017
PRIM_ADDRESS.State,-----------------------------------------------------------------------------------------------------------------------added by Oleg 5/1/2017
PRIM_ADDRESS.County,-----------------------------------------------------------------------------------------------------------------------added by Oleg 5/1/2017
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription <> 'Expense' WHERE RP.ClaimId = C.ClaimId AND IsPayment = 1 AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [ClaimsPaid],
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription <> 'Expense' WHERE RP.ClaimId = C.ClaimId AND IsPayment = 0 AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [ClaimsReserveRemaining],
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription = 'Expense' WHERE RP.ClaimId = C.ClaimId AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0)AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [AllocatedReserved],
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription = 'Expense' WHERE RP.ClaimId = C.ClaimId AND IsPayment = 1 AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [AllocatedPaid],
(SELECT ISNULL(SUM(ResPayAmount), 0) FROM tblClaims_ReservePayments RP INNER JOIN lstClaims_ReservePaymentTypes RPT ON RPT.ResPayTypeId = RP.ResPayTypeId AND RPT.ResPayTypedescription = 'Expense' WHERE RP.ClaimId = C.ClaimId AND IsPayment = 0 AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND CASE WHEN #coverageTypeId IS NULL THEN 1 ELSE CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1) AS [AllocatedReserveRemaining],
(SELECT ISNULL(SUM(Amount), 0) FROM tblClaims_ClaimExpenses CE WHERE CE.ClaimId = C.ClaimId AND (DATEDIFF(d, #dateFrom, CE.DateEntered) >= 0 AND DATEDIFF(d, #dateTo, CE.DateEntered) <= 0)) AS [UnAllocated]
FROM
tblClaims_Claim C
INNER JOIN
tblClaims_PolicyInformation CPI
ON
CPI.ClaimId = C.ClaimId
INNER JOIN tblClaims_Claimants CC ON C.ClaimId = CC.ClaimId --AND CC.StatusId = 0 -----------------------------------added by Oleg 5/1/2017
INNER JOIN tblClaims_Addresses PRIM_ADDRESS ON PRIM_ADDRESS.AddressId = CC.AddressId ----------------------------------added by Oleg 5/1/2017
CROSS APPLY (SELECT TOP 1 QuoteGUID FROM tblQuotes WHERE tblQuotes.ControlNo = C.ControlNo) t ------------------------added by Oleg 5/1/2017
WHERE EXISTS
(SELECT * FROM tblClaims_ReservePayments RP
WHERE
CASE WHEN #coverageTypeId IS NULL THEN 1
ELSE
CASE WHEN RP.CoverageTypeId = #coverageTypeId THEN 1 ELSE 0 END END = 1
AND (DATEDIFF(d, #dateFrom, RP.DateCreated) >= 0 AND DATEDIFF(d, #dateTo, RP.DateCreated) <= 0) AND RP.ClaimId = C.ClaimId)
AND
CASE WHEN #companyGuid IS NULL THEN 1 ELSE CASE WHEN CPI.CompanyGuid = #companyGuid THEN 1 ELSE 0 END END = 1
AND
EXISTS (SELECT * FROM tblClaims_Claimants CLMNT WHERE CLMNT.ClaimID = C.ClaimID AND CLMNT.StatusId = 0)
AND EXISTS (SELECT * FROM #Adjusters WHERE adjuster = C.InhouseAdjuster)
ORDER BY
CPI.PolicyNumber
UPDATE:
If i put extra space in any line - it changes the performance to 7 sec.
When a query is run, SQL Server compiles it into a query plan.
In order to match the same SQL query to this plan, the database generates a hash code for the query (called a "query hash") based on the query text.
If you change the query in any way-- even seemingly safe things like capitalization (where this does not affect the query), add spaces, add comments-- the query text is now different. SQL Server will generate a different query hash for the query.
Given a new query hash, an existing query plan for this query hash will not be found. Therefore the SQL optimizer will create a new query plan. The new query plan is most likely very different than the old one-- possibly from parameter sniffing, as you suggested.
Commenting out functional parts of the query does actually change the query. For example, commenting out a single line in a SELECT clause can drastically alter the performance of the query, because now SQL Server may not need that column from the table. This means it could select a different index or skip a bookmark lookup, so performance can radically change.
I am getting the following area message when executing this SQL query:
Incorrect syntax near the keyword 'else'
Here is the query:
SELECT
*,
CASE
WHEN los IN (0, 1, 2, 3, 4) THEN 'under 5'
WHEN los >= 5 THEN 'above 5'
ELSE 'Unknown'
END AS los_type
FROM
(SELECT
CASE
WHEN LEN(comp.AGTNUMBER) = 6 THEN substring(comp.AGTNUMBER, 2, 5)
ELSE comp.AGTNUMBER
END AS FR_Number,
comp.COMPLAINTNUMBER,
YEAR(comp.opendate) AS Comp_Year,
comp.COMPLAINTORIGIN,
comp.COMPTYPE AS Comp_Type,
comp.OPENDATE,
comp.REBUTTAL,
comp.AGENTNAMEINCOM,
CASE
WHEN fr.FULL_TIME_SVC_DTE < comp.OPENDATE
THEN FLOOR(DATEDIFF(MONTH, fr.FULL_TIME_SVC_DTE, comp.OPENDATE) / 12)
WHEN fr.FULL_TIME_SVC_DTE > comp.OPENDATE
THEN floor(datediff(month, fr.ACTUAL_CONTRACT_END_DATE, fr.FULL_TIME_SVC_DTE) / 12
ELSE NULL
END AS los
FROM
dbo.EC_Complaints_Info AS comp
LEFT JOIN
dbo.FLD_REP_Info AS fr
ON CASE WHEN LEN(comp.AGTNUMBER) = 6
THEN substring(comp.AGTNUMBER, 2, 5)
ELSE comp.AGTNUMBER
END = fr.FR_NUMBER
AND comp.OPENDATE BETWEEN fr.ACTUAL_CONTRACT_START_DATE
AND fr.ACTUAL_CONTRACT_END_DATE
WHERE
((comp.REBUTTAL = 'No'))
AND ((comp.STATUS = 'open') OR
(comp.STATUS = 'closed'))
) AS a
Can anyone help me figure out why I'm getting this error message?
You are missing a ) in one of your CASE expressions:
case when fr.FULL_TIME_SVC_DTE < comp.OPENDATE then floor(datediff(month, fr.FULL_TIME_SVC_DTE, comp.OPENDATE)/12)
when fr.FULL_TIME_SVC_DTE > comp.OPENDATE
then floor(datediff(month, fr.ACTUAL_CONTRACT_END_DATE, fr.FULL_TIME_SVC_DTE)/12 --here is the error
else null end as los
It should be:
case when fr.FULL_TIME_SVC_DTE < comp.OPENDATE then floor(datediff(month, fr.FULL_TIME_SVC_DTE, comp.OPENDATE)/12)
when fr.FULL_TIME_SVC_DTE > comp.OPENDATE
then floor(datediff(month, fr.ACTUAL_CONTRACT_END_DATE, fr.FULL_TIME_SVC_DTE)/12)
else null end as los
I am having trouble converting this Pervasive sql query to Oracle. The errors that oracle is giving me does not help. It is giving me an error at SQL_Date.
This is what I have in Pervasive:
SELECT
(CASE
WHEN joefdt = 0
THEN NULL
WHEN joefdt > 0
THEN CONVERT(LEFT(CONVERT(joefdt, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(joefdt, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(joefdt, SQL_CHAR),2),SQL_DATE) END) AS EFFECTIVEDATE,
(CASE
WHEN jopost = 0
THEN NULL
WHEN jopost > 0
THEN CONVERT(LEFT(CONVERT(jopost, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(jopost, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(jopost, SQL_CHAR),2),SQL_DATE) END) AS POSTDATE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 3 AND cdnumb = joetyp) AS TYPE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 5 AND cdnumb = jodelm) AS DELIVERYMETHOD,
joissd AS SHARESISSUED, jocand AS SHARESCANCELLED , joprsn AS BROKERID, joprsn AS TRANSACID
FROM money.jour
WHERE joiss = 1 AND joetyp in (2,3,4) AND (JOISSD > 0 OR JOCAND > 0)
AND joefdt >= '19920127' AND joefdt <= '20040726'
ORDER BY joefdt;
This is what I have tried, but it doesn't work:
SELECT
(CASE
WHEN joefdt = 0
THEN NULL
WHEN joefdt > 0
THEN CONVERT(substr(CONVERT(joefdt, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(joefdt, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(joefdt, SQL_CHAR),2),SQL_DATE) END) AS EFFECTIVEDATE,
(CASE
WHEN jopost = 0
THEN NULL
WHEN jopost > 0
THEN CONVERT(substr(CONVERT(jopost, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(jopost, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(jopost, SQL_CHAR),2),SQL_DATE) END) AS POSTDATE, ----Error at SQL_DATE
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 3 AND cdnumb = joetyp) AS TYPE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 5 AND cdnumb = jodelm) AS DELIVERYMETHOD,
joissd AS SHARESISSUED, jocand AS SHARESCANCELLED , joprsn AS BROKERID, joprsn AS TRANSACID
FROM money.jour
WHERE joiss = 1 AND joetyp in (2,3,4) AND (JOISSD > 0 OR JOCAND > 0)
AND joefdt >= '20000302' AND joefdt <= '20000302'
ORDER BY joefdt;
In Oracle CONVERT function translates character string from one character set to another. It cannot be used to convert something to date (does Pervasive means this?) or something. Oracle does not know identifiers SQL_DATE and SQL_CHAR, so you cannot use them in this manner. And last, conditions like joefdt >= '20000302' are doubtful. Maybe it means something like joefdt >= DATE '2000-03-02'