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
Related
Hello two days I have tried to figure out why my code isn't working.
I am quite sure that it's a stupid mistake, but if anybody has time to check the code I would really appreciate.
I have tried to comment out this "SUM" case, but then everything else breaks
I know that this line screws with me, but I can't figure out how to fix this:
sum (Case when bm.CaseQTY IS NULL
then (k.postedqty / mm.CaseQTY)
else (k.postedqty / bm.CaseQTY)
End) as laoseiskaste,
sum (Case when bm.CaseQTY IS NULL
then ((k.postedqty-k.reservordered-k.reservphysical-k.picked+k.registered) / mm.CaseQTY)
else ((k.postedqty-k.reservordered-k.reservphysical-k.picked+k.registered) / bm.CaseQTY)
End) as vabakaste
from (select s.itemid, i.product, i.ItemMiscRefId, i.MMFItemProductGroupId, s.INVENTDIMID,
s.postedqty, s.registered, s.reservordered, s.reservphysical, s.picked, s.ordered,
s.postedvalue, s.received, i.PrimaryVendorId, i.standardPalletQuantity
from inventsum s WITH (INDEX(I_174CLOSEDITEMDIMIDX),NOLOCK),
SELECT k.itemid AS itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END) AS mmfitemid,
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END) AS itembarcode,
e.name AS name, d.inventlocationid AS inventlocationid, d.inventbatchid AS inventbatchid,
LEFT(CONVERT(varchar, ib.proddate, 121), 10) AS proddate,
LEFT(CONVERT(varchar, ib.expdate, 121), 10) AS expdate,
d.wmslocationid AS wmslocationid, d.wmspalletid AS wmspalletid, p.pallettypeid AS pallettypeid,
xz.inputblockingcauseid AS inputblockingcauseid, xz.outputblockingcauseid AS outputblockingcauseid,
d.inventserialid AS inventserialid, d.inventownerid_via AS inventownerid_via,
d.inventlabelid_via AS inventlabelid_via, d.inventtaxstampid AS inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END) AS caseqty,
k.standardPalletQuantity AS palletqty,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END) AS caseheight,
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END) AS casewidth,
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END) AS casedepth,
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END) AS casebarcode,
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END) AS casebrutoweight,
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END) AS alcopct,
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END) AS blockqty,
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END) AS exciseproductregnum,
SUM(k.postedqty + k.received + k.registered) AS postedqty,
SUM(k.registered) AS registeredqty,
SUM(k.reservordered + k.reservphysical) AS reservedqty, SUM(k.picked) AS pickedqty,
SUM(k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) AS freeqty,
SUM(k.ordered) AS ordered, SUM(k.postedvalue) AS postedvalue,
k.MMFItemProductGroupId AS mmfitemproductgroupid,
SUM(CASE WHEN bm.CaseQTY IS NULL
THEN (k.postedqty / mm.CaseQTY)
ELSE (k.postedqty / bm.CaseQTY)
END) AS laoseiskaste,
SUM(CASE WHEN bm.CaseQTY IS NULL
THEN ((k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) / mm.CaseQTY)
ELSE ((k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) / bm.CaseQTY)
END) AS vabakaste
FROM (SELECT s.itemid, i.product, i.ItemMiscRefId, i.MMFItemProductGroupId, s.INVENTDIMID,
s.postedqty, s.registered, s.reservordered, s.reservphysical, s.picked,
s.ordered, s.postedvalue, s.received, i.PrimaryVendorId, i.standardPalletQuantity
FROM inventsum s WITH (INDEX(I_174CLOSEDITEMDIMIDX), NOLOCK),
inventtable i WITH (INDEX([I_175ITEMIDX]), NOLOCK)
WHERE s.itemid = i.itemid
AND s.DATAAREAID = #Var1
AND i.DATAAREAID = #Var1
AND s.PARTITION = 5637144576
AND i.PARTITION = 5637144576
AND RIGHT(s.ITEMID, 2) = 'LA'
AND s.CLOSED = 0
AND (s.POSTEDQTY != 0
OR s.reservordered + s.reservphysical != 0
OR s.ordered != 0
)
) k
JOIN inventdim d WITH (INDEX(I_698DIMIDIDX), NOLOCK)
ON (k.INVENTDIMID = d.INVENTDIMID
AND d.DATAAREAID = #Var3
AND d.PARTITION = 5637144576)
LEFT OUTER JOIN EcoresProductTranslation e WITH (INDEX(I_6869PRODUCTLANGUAGEIDX))
ON (k.product = e.product
AND e.languageid = 'et'
AND e.PARTITION = 5637144576)
LEFT OUTER JOIN WMSPALLET p
ON (p.WMSPALLETID = d.WMSPALLETID
AND p.PARTITION = 5637144576
AND p.DATAAREAID = #Var4)
LEFT OUTER JOIN InventItemMisc mm WITH (INDEX(I_103616RecId))
ON (mm.RecId = k.ItemMiscRefId
AND mm.PARTITION = 5637144576)
LEFT OUTER JOIN InventBatch ib WITH (INDEX(I_752itemBatchIdx))
ON (ib.ItemId = k.ItemId
AND ib.InventBatchId = d.InventBatchId
AND ib.PARTITION = 5637144576
AND ib.DATAAREAID = #Var5)
LEFT OUTER JOIN InventItemMisc bm WITH (INDEX(I_103616RecId))
ON (bm.RecId = ib.ItemMiscRefId
AND bm.PARTITION = 5637144576)
JOIN WMSLOCATIOn xz WITH (INDEX(/*I672COORDINATEDIDX*/ I_672LOCATIONIDX))
ON (xz.inventlocationID = d.inventlocationID
AND xz.wmslocationid = d.wmslocationid
AND xz.partition = 5637144576
AND xz.dataareaid = 'EWH')
WHERE 1 = 1
GROUP BY k.itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END),
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END),
e.name, d.inventlocationid, d.inventbatchid, LEFT(CONVERT(varchar, ib.proddate, 121), 10),
LEFT(CONVERT(varchar, ib.expdate, 121), 10), d.wmslocationid, d.wmspalletid,
p.pallettypeid, xz.inputblockingcauseid, xz.outputblockingcauseid, d.inventserialid,
d.inventownerid_via, d.inventlabelid_via, d.inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END),
k.standardPalletQuantity,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END),
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END),
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END),
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END),
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END),
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END),
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END),
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END),
k.MMFItemProductGroupId
ORDER BY k.itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END),
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END),
e.name, d.inventlocationid, d.inventbatchid, LEFT(CONVERT(varchar, ib.proddate, 121), 10),
LEFT(CONVERT(varchar, ib.expdate, 121), 10), d.wmslocationid, d.wmspalletid,
p.pallettypeid, xz.inputblockingcauseid, xz.outputblockingcauseid,
d.inventserialid, d.inventownerid_via, d.inventlabelid_via, d.inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END),
k.standardPalletQuantity,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END),
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END),
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END),
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END),
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END),
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END),
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END),
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END),
k.MMFItemProductGroupId;
Error in SQL:Error in SQL:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Divide by zero error encountered.
In SSMS, hit Ctrl+F to open the Find & Replace dialogue box. Search the query for all instances of /. This will tell you everywhere that division is happening, plus some false positives.
In every single instance, the query is dividing by mm.CaseQTY or bm.CaseQTY. Those are the only two values, and the only two tables you have to worry about. One or the other of them is coming through as a zero.
We don't have access to your database, so we can't help you troubleshoot the data issue, but that's how I'd go about deciding where to start looking.
I am working with a legacy system that only accepts dd/mm/yyyy in the XML for the data fields otherwise it mucks up the data entry.
I have tried every which way to attempt to force my data fields to display as dd/mm/yyyy, I have attempted to CONVERT to DATETIME 103, I have tried declaring it as a VARCHAR and everytime time it returns as 2018-05-11 for example.
How can I force the returned field to display as DD/MM/YYYY, this is a snippet of my code, excuse how messy it is unfortunately it is very linear and the system that accepts the XML is very basic.
SELECT TOP 1 1 'job/queue',
#Branch 'job/branch',
CASE WHEN myli.PolRef# IS NOT NULL THEN 'update-broomsrisk' ELSE 'create-broomsrisk' END 'parameters/yzt/char20.1',
SUBSTRING(#PolicyRef,1,6) 'broomsdata/broomsclient/bcm/refno',
#PolicyRef 'broomsdata/broomspolicy/bpy/refno',
CASE WHEN myli.PolRef# IS NOT NULL THEN myli.key# ELSE NULL END 'broomsdata/broomspolicy/myli/KEY',
(SELECT ISNULL(ct2.convictiondate,ct2.offencedate) FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 1) 'broomsdata/broomspolicy/myli/Date1',
(SELECT ct2.code FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 1) 'broomsdata/broomspolicy/myli/Code1',
(SELECT ct2.fine FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 1) 'broomsdata/broomspolicy/myli/Fine1',
(SELECT ct2.noofpoints FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 1) 'broomsdata/broomspolicy/myli/Points1',
(SELECT ISNULL(ct2.convictiondate,ct2.offencedate) FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 2) 'broomsdata/broomspolicy/myli/Date2',
(SELECT ct2.code FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 2) 'broomsdata/broomspolicy/myli/Code2',
(SELECT ct2.fine FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 2) 'broomsdata/broomspolicy/myli/Fine2',
(SELECT ct2.noofpoints FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 2) 'broomsdata/broomspolicy/myli/Points2',
(SELECT ISNULL(ct2.convictiondate,ct2.offencedate) FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 3) 'broomsdata/broomspolicy/myli/Date3',
(SELECT ct2.code FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 3) 'broomsdata/broomspolicy/myli/Code3',
(SELECT ct2.fine FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 3) 'broomsdata/broomspolicy/myli/Fine3',
(SELECT ct2.noofpoints FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 3) 'broomsdata/broomspolicy/myli/Points3',
(SELECT ISNULL(ct2.convictiondate,ct2.offencedate) FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 4) 'broomsdata/broomspolicy/myli/Date4',
(SELECT ct2.code FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 4) 'broomsdata/broomspolicy/myli/Code4',
(SELECT ct2.fine FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 4) 'broomsdata/broomspolicy/myli/Fine4',
(SELECT ct2.noofpoints FROM #ConvictionTable AS ct2 WHERE ct2.RowNo = 4) 'broomsdata/broomspolicy/myli/Points4'
FROM #ConvictionTable AS ct
LEFT OUTER JOIN [FIG-SQL-SRV].InfoCentre.dbo.ic_BD_MYLI AS myli ON ct.branch = myli.B# AND ct.policyRef = myli.PolRef#
FOR XML PATH('xmlexecute'), TYPE;
The Fields I'm trying to force are the 'Date1'/'Date2' etc. fields.
Cheers
Use CONVERT(varchar(10), YourDateColumn, 103). I think your mistake was trying to convert to a datetime (I'm guessing you tried CONVERT(datetime, YourDateColumn, 103)) while using a style code; date/time datatypes, in SQL Server, have no format. Trying to format them won't do anything.
For example: SELECT CONVERT(varchar(10), GETDATE(), 103); returns the varchar(10) value '11/05/2018'.
Edit: The below is to (temporary) assist the OP in showing how i create XML for OpenInterchange. The OP is using a lot of Sub Selects, something that doesn't help the speed of the query. I've highlighted, a couple of points for the OP to note as well:
SELECT K.Insco AS Insurer,
K.B# AS Branch,
K.PolRef# AS PolicyRef,
K.ClaimRef# AS ClaimRef,
--XML Generation starts here.
(SELECT(SELECT K.B# AS branch,
'OO' AS operator,
'PDF' AS printtype
FOR XML PATH('job'),TYPE),
(SELECT (SELECT 'update-claim' AS [Char20.1]
FOR XML PATH('yzt'),TYPE)
FOR XML PATH('parameters'),TYPE),
(SELECT (SELECT (SELECT K.Ref# AS Refno
FOR XML PATH('bcm'),TYPE)
FOR XML PATH('broomsclient'),TYPE),
(SELECT (SELECT K.PolRef# AS Refno
FOR XML PATH('bpy'),TYPE)
FOR XML PATH('broomspolicy'),TYPE),
(SELECT (SELECT K.ClaimRef# AS [Claim.No],
K.[Claim No] AS [Ins.Claim.Ref],
K.KGMSettled AS [Claim.Settled],
K.KGMReserve AS [Claim.Reserve],
K.KGMReserve + K.KGMSettled As [Claim.Amount],
K.KGMStatus AS [Stat.Desc],
CASE K.KGMStatus WHEN 'Settled' THEN 'SETT'
WHEN 'Ouststanding' THEN 'OUTS'
WHEN 'Declined' THEN 'DECL'
WHEN 'Not Taken up' THEN 'NTUP'
WHEN 'Withdrawn' THEN 'WITH' END AS [stat.code],
CONVERT(varchar(10),K.[Notified Date],103) AS [Date.repd], --First example of the dd/MM/yyyy format
CONVERT(varchar(10),K.[Loss Date],103) AS [Loss.date], --Another example of the dd/MM/yyyy format
CASE WHEN K.[Date Closed] IS NOT NULL THEN CONVERT(varchar(10), GETDATE(), 103) END AS [Stat.date], -- and another example of the dd/MM/yyyy format
CONVERT(varchar(10),K.[Date Closed],103) AS [Date.Settled], --you get the idea. :)
CASE WHEN K.[Date Closed] IS NOT NULL AND K.[Incident Type] = 'Windscreen' THEN 'Yes'
WHEN K.[Date Closed] IS NULL THEN ''
WHEN K.[NCB Decision] = 'Allowed' THEN 'Yes'
WHEN K.[NCB Decision] = 'Disallowed' THEN 'Yes' END AS [Settled.free]
/*
CASE WHEN K.[Date Closed] IS NOT NULL AND K.[Incident Type] = 'Windscreen' THEN 'Yes'
WHEN K.KGMReserve + K.KGMSettled = 0 AND K.[Date Closed] IS NOT NULL THEN 'Yes'
WHEN K.KGMReserve + K.KGMSettled > 0 AND K.[Date Closed] IS NOT NULL THEN 'No'
ELSE '' END AS [Settled.free]*/
FOR XML PATH('bch'),TYPE),
(SELECT K.BCAKey AS [KEY],
K.[Vehicle Reg# No] AS [Reg.no],
K.[Driver Name] AS [Driver],
K.[ AD Paid] AS [Pay.ad],
K.[AD Reserve] AS [Reserve.ad],
K.[TPD Paid] + K.[TPI Paid] AS [Pay.tp],
K.[TPD Reserve] + K.[TPI Reserve] AS [Reserve.tp],
--K.[TPI Paid] AS [Pay.others],
--K.[TPI Reserve] AS [Reserve.others],
0 AS [Pay.others],
0 AS [Reserve.others],
K.KGMSettled AS [Pay.total],
K.KGMReserve AS [Reserve.Total],
K.[Recovery] AS [Pay.recovs],
K.[AD Recovery Reserve] AS [Reserve.recovs],
'Updated by FileDrop Service. XML Generated on ' + CONVERT(varchar(10),GETDATE(),103) as [Remarks4]
FOR XML PATH('bca'),TYPE),
(SELECT CONVERT(varchar(10),GETDATE(),103) AS [date],
LEFT(CONVERT(varchar(10),GETDATE(),108),5) AS [time],
'OO' AS [op],
'FileDrop' AS [Name],
K.KGMSettled AS [Settled],
K.KGMReserve AS [Reserve],
K.KGMSettled + K.KGMReserve As [Total],
--CONVERT(varchar(10),K.DataToDate,103) AS [EffDate],
CASE WHEN K.KGMSettled != K.BCHSettled THEN 'Payment'
WHEN K.KGMReserve != K.BCHReserve THEN 'Reserve Adjustment'
ELSE 'Payment' END AS [Notes]
FOR XML PATH('clam'),TYPE)
FOR XML PATH('broomsclaim'),TYPE)
FOR XML PATH('broomsdata'),TYPE)
FOR XML PATH('xmlexecute'),TYPE) AS InputXML,
GETDATE() AS DateGenerated,
0 AS TestLoad
FROM KGMCGStandard_viw K
WHERE K.ClaimsFound = 1
AND K.KGMFound = 1
AND (K.KGMSettled != K.BCHSettled
OR K.KGMReserve != K.BCHReserve
OR (CONVERT(date,K.KGMClosure) != CONVERT(date,BCHClosure)
OR (K.KGMClosure IS NULL AND K.BCHClosure IS NOT NULL)
OR (K.KGMClosure IS NOT NULL AND K.BCHClosure IS NULL))
OR (CASE WHEN K.[Date Closed] IS NOT NULL AND K.[Incident Type] = 'Windscreen' THEN 'Yes'
WHEN K.[NCB Decision] = 'Allowed' THEN 'Yes'
WHEN K.[NCB Decision] = 'Disallowed' THEN 'No' END != K.Settled_Free
OR (K.[Date Closed] IS NULL AND K.Settled_free IS NULL)))
AND NOT EXISTS (SELECT 1
FROM AutoloadXML_tbl A
WHERE A.Branch = K.B#
AND A.PolicyRef = K.PolRef#
AND A.ClaimRef = K.ClaimRef#
AND (A.DateSubmitted IS NULL
OR A.Installed = 0))
ORDER BY K.ClaimRef# ASC;
Since you already figured out the date part I will leave that up to you but your query could be simplified majorly using conditional aggregation. I used square braces around the column names instead of single quotes. Sure those work but I consider that a bad habit because it is difficult to decide if it is a string literal or a column alias. I also prefer to use alias = but that is more a preference thing. I also prefer leading commas which is also a preference thing.
Pretty sure this query should do the same thing.
SELECT 1 as 'job/queue',
#Branch as 'job/branch',
CASE WHEN myli.PolRef# IS NOT NULL THEN 'update-broomsrisk' ELSE 'create-broomsrisk' END as 'parameters/yzt/char20.1',
SUBSTRING(#PolicyRef,1,6) as 'broomsdata/broomsclient/bcm/refno',
#PolicyRef as 'broomsdata/broomspolicy/bpy/refno',
CASE WHEN myli.PolRef# IS NOT NULL THEN myli.key# ELSE NULL END as 'broomsdata/broomspolicy/myli/KEY'
, [broomsdata/broomspolicy/myli/Date1] = max(case when ct.RowNo = 1 then ISNULL(ct.convictiondate, ct.offencedate) end)
, [broomsdata/broomspolicy/myli/Code1] = max(case when ct.RowNo = 1 then ct.code end)
, [broomsdata/broomspolicy/myli/Fine1] = max(case when ct.RowNo = 1 then ct.fine end)
, [broomsdata/broomspolicy/myli/Points1] = max(case when ct.RowNo = 1 then ct.noofpoints end)
, [broomsdata/broomspolicy/myli/Date2] = max(case when ct.RowNo = 2 then ISNULL(ct.convictiondate, ct.offencedate) end)
, [broomsdata/broomspolicy/myli/Code2] = max(case when ct.RowNo = 2 then ct.code end)
, [broomsdata/broomspolicy/myli/Fine2] = max(case when ct.RowNo = 2 then ct.fine end)
, [broomsdata/broomspolicy/myli/Points] = max(case when ct.RowNo = 2 then ct.noofpoints end)
, [broomsdata/broomspolicy/myli/Date3] = max(case when ct.RowNo = 3 then ISNULL(ct.convictiondate, ct.offencedate) end)
, [broomsdata/broomspolicy/myli/Code3] = max(case when ct.RowNo = 3 then ct.code end)
, [broomsdata/broomspolicy/myli/Fine3] = max(case when ct.RowNo = 3 then ct.fine end)
, [broomsdata/broomspolicy/myli/Points3] = max(case when ct.RowNo = 3 then ct.noofpoints end)
, [broomsdata/broomspolicy/myli/Date4] = max(case when ct.RowNo = 4 then ISNULL(ct.convictiondate, ct.offencedate) end)
, [broomsdata/broomspolicy/myli/Code4] = max(case when ct.RowNo = 4 then ct.code end)
, [broomsdata/broomspolicy/myli/Fine4] = max(case when ct.RowNo = 4 then ct.fine end)
, [broomsdata/broomspolicy/myli/Points4] = max(case when ct.RowNo = 4 then ct.noofpoints end)
FROM #ConvictionTable AS ct
LEFT OUTER JOIN [FIG-SQL-SRV].InfoCentre.dbo.ic_BD_MYLI AS myli ON ct.branch = myli.B# AND ct.policyRef = myli.PolRef#
GROUP BY #Branch
, CASE WHEN myli.PolRef# IS NOT NULL THEN 'update-broomsrisk' ELSE 'create-broomsrisk' END
, SUBSTRING(#PolicyRef,1,6)
, #PolicyRef
, CASE WHEN myli.PolRef# IS NOT NULL THEN myli.key# ELSE NULL END
FOR XML PATH('xmlexecute'), TYPE
can anyone see what keyword is missing from the pl/sql query below?
I am getting the error ORA-00905: missing keyword (microsoft ole db provider for oracle)
not sure what keyword is missing - this is an odbc query to oracle database
SELECT "STAT"."ORD"."SAMPLEID",
MAX(CASE WHEN "STAT"."ORDANAEL"."MC"='QAERS' THEN N"STAT"."ORDANAEL"."RES_TXT" ELSE
'' END) AS ERSID,
"STAT"."ORDMCSTP_V"."SEQNB", "STAT"."ORD"."ORDPATNAME",
"STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB", "STAT"."ORD"."PROJNB",
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN 'AMMOL'
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN 'AMMOLR' ELSE '' END) AS TEST,
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N"STAT"."ORDRESMCEL"."MC_RES"
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN N"STAT"."ORDRESMCEL"."MC_RES" ELSE ''
END) AS MC_RES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMI' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMI,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMP' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMP,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMNDM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS NDM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMOX48' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS OXA48,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSPM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SPM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMVIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS VIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMDIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS DIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMKPC' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS KPC,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGES' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSME' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SME,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMFRI1' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS FRI1,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GIM,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N "STAT"."ORDANAEL"."VALMED" ELSE
'' END) AS VALMED_AMMOL,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' AND
"STAT"."ORDANAEL_D"."DFIELD"='RR' THEN N "STAT"."ORDANAEL_D"."DVALUE" ELSE '' END)
AS RPT_DT_AMMOL,
"STAT"."ORDANAEL"."RES_ST"
FROM (("STAT"."ORDMCSTPEL" INNER JOIN ("STAT"."ORDMCSTP_V" INNER JOIN
("STAT"."ORD" INNER JOIN "STAT"."ORDANAEL" ON ("STAT"."ORD"."LID" =
"STAT"."ORDANAEL"."LID") AND ("STAT"."ORD"."LPERIOD" =
"STAT"."ORDANAEL"."LPERIOD") AND ("STAT"."ORD"."ORDNB" =
"STAT"."ORDANAEL"."ORDNB")) ON ("STAT"."ORDMCSTP_V"."LID" = "STAT"."ORD"."LID")
AND ("STAT"."ORDMCSTP_V"."LPERIOD" = "STAT"."ORD"."LPERIOD") AND
("STAT"."ORDMCSTP_V"."ORDNB" = "STAT"."ORD"."ORDNB")) ON ("STAT"."ORDMCSTPEL"."BC"
= "STAT"."ORDMCSTP_V"."BC") AND ("STAT"."ORDMCSTPEL"."CURRENT_STEPNB" =
"STAT"."ORDMCSTP_V"."CURRENT_STEPNB")) INNER JOIN "STAT"."ORDANAEL_D" ON
("STAT"."ORDANAEL"."NBINT" = "STAT"."ORDANAEL_D"."DNBINT") AND
("STAT"."ORDANAEL"."SPSEQ" = "STAT"."ORDANAEL_D"."DSPSEQ") AND
("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDANAEL_D"."DORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDANAEL_D"."DLPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDANAEL_D"."DLID")) INNER JOIN
"STAT"."ORDRESMCEL" ON ("STAT"."ORDMCSTP_V"."SEQNB" = "STAT"."ORDRESMCEL"."SEQNB")
AND ("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDRESMCEL"."ORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDRESMCEL"."LPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDRESMCEL"."LID")
WHERE (("STAT"."ORD"."LID")='H1')
AND (("STAT"."ORD"."LPERIOD")>'5200')
AND (("STAT"."ORD"."PROJNB")='ACPOERS')
GROUP BY "STAT"."ORD"."SAMPLEID", "STAT"."ORDMCSTP_V"."SEQNB",
"STAT"."ORD"."ORDPATNAME", "STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB",
"STAT"."ORD"."PROJNB", "STAT"."ORDANAEL"."RES_ST"
HAVING ((("STAT"."ORDMCSTP_V"."SEQNB") Is Not Null) AND (("STAT"."ORD"."PROJNB")
='ACPOERS') AND (("STAT"."ORDANAEL"."RES_ST")>'4'))
Just too big for comment. Just cleaned up the double quotes and the N and it comes clean in a SQL formatter.
SELECT STAT.ORD.SAMPLEID,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'QAERS' THEN STAT.ORDANAEL.RES_TXT ELSE '' END ) AS ersid,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN 'AMMOL'
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN 'AMMOLR'
ELSE ''
END )
AS test,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDRESMCEL.MC_RES
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN STAT.ORDRESMCEL.MC_RES
ELSE ''
END )
AS mc_res,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMI' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imi,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMP' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imp,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMNDM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ndm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMOX48' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS oxa48,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSPM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS spm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMVIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS vim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMDIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS dim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMKPC' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS kpc,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGES' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ges,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSME' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sme,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMFRI1' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS fri1,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS gim,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDANAEL.VALMED ELSE '' END )
AS valmed_ammol,
MAX ( CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL'
AND STAT.ORDANAEL_D.DFIELD = 'RR' THEN
STAT.ORDANAEL_D.DVALUE
ELSE
''
END )
AS rpt_dt_ammol,
STAT.ORDANAEL.RES_ST
FROM ((STAT.ORDMCSTPEL
INNER JOIN (STAT.ORDMCSTP_V
INNER JOIN (STAT.ORD
INNER JOIN STAT.ORDANAEL
ON ( STAT.ORD.LID = STAT.ORDANAEL.LID )
AND ( STAT.ORD.LPERIOD = STAT.ORDANAEL.LPERIOD )
AND ( STAT.ORD.ORDNB = STAT.ORDANAEL.ORDNB ))
ON ( STAT.ORDMCSTP_V.LID = STAT.ORD.LID )
AND ( STAT.ORDMCSTP_V.LPERIOD = STAT.ORD.LPERIOD )
AND ( STAT.ORDMCSTP_V.ORDNB = STAT.ORD.ORDNB ))
ON ( STAT.ORDMCSTPEL.BC = STAT.ORDMCSTP_V.BC )
AND ( STAT.ORDMCSTPEL.CURRENT_STEPNB = STAT.ORDMCSTP_V.CURRENT_STEPNB ))
INNER JOIN STAT.ORDANAEL_D
ON ( STAT.ORDANAEL.NBINT = STAT.ORDANAEL_D.DNBINT )
AND ( STAT.ORDANAEL.SPSEQ = STAT.ORDANAEL_D.DSPSEQ )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDANAEL_D.DORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDANAEL_D.DLPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDANAEL_D.DLID ))
INNER JOIN STAT.ORDRESMCEL
ON ( STAT.ORDMCSTP_V.SEQNB = STAT.ORDRESMCEL.SEQNB )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDRESMCEL.ORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDRESMCEL.LPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDRESMCEL.LID )
WHERE ( ( STAT.ORD.LID ) = 'H1' )
AND ( ( STAT.ORD.LPERIOD ) > '5200' )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
GROUP BY STAT.ORD.SAMPLEID,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
STAT.ORDANAEL.RES_ST
HAVING ( ( ( STAT.ORDMCSTP_V.SEQNB ) IS NOT NULL )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
AND ( ( STAT.ORDANAEL.RES_ST ) > '4' ) );
You can use N in front of a string literal to mark it as an nvarchar2. Example:
select N'abc' from dual
But you can't use it in front of an identifier the way you are doing it. If you want to read a certain column value as an nvarchar2, you'll need to cast it as such. Example:
... THEN cast("STAT"."ORDANAEL"."RES_TXT" as nvarchar2(50)) ...
I have data like the following:
And what I want is to count the PONo, PartNo, and TrinityID fields with a value in them, and output data like this:
How can I do this counting in SQL?
select
Job_number, Item_code,
case when RTRIM(PONo) = '' or PONo is null then 0 else 1 end +
case when RTRIM(PartNo) = '' or PartNo is null then 0 else 1 end +
case when RTRIM(TrinityID) = '' or TrinityID is null then 0 else 1 end
as [Count]
from YourTable
Try this:
select Job_Number = t.Job_Number ,
Item_Code = t.Item_Code ,
"Count" = sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
)
from dbo.my_table t
group by t.Job_Number , t.Item_Code
If you want to exclude data where all the tested fields are null or empty, add a having clause:
having sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
) > 0
Easy!
After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.