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 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
I want to add a new column called Order_No from a table not currently in the query below : ordloc_discount
The four columns are
deal_id
item
location
last_calc_date
SELECT idh.vendor supplier
,idh.deal_id
,mff_report.mff_merch_sql.get_sup_name(idh.vendor) sup_name
,idh.deal_type
,scdhh.start_invoice_date
,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY') start_invoice_date_char
,idh.doc_date
,TO_CHAR(idh.doc_date,'MM/DD/YYYY') doc_date_char
,idh.ext_doc_id
,NULL deal_desc
,idh.custom_doc_ref_2 ext_ref_no
,idh.custom_doc_ref_3 deal_comp_type_desc
,idh.custom_doc_ref_4 comments
,idh.total_qty
,idh.total_cost
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.loc_type
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.loc_type
ELSE icdd.loc_type
END loc_type
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.location
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.location
ELSE icdd.location
END location
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN vl.loc_name
WHEN 'P' THEN NULL
WHEN 'PL' THEN vl.loc_name
ELSE vl.loc_name
END loc_name
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item,im.item)
WHEN 'PL' THEN NVL(im_parent.item,im.item)
ELSE im.item
END item
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
WHEN 'PL' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
END brand_name
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item_desc,im.item_desc)
WHEN 'PL' THEN NVL(im_parent.item_desc,im.item_desc)
ELSE im.item_desc
END item_desc
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'VL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'P' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
WHEN 'PL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
ELSE mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
END vpn
,SUM(icdd.actual_turnover_units) turnover_units
,SUM(icdd.actual_turnover_revenue) turnover_revenue
,SUM(icdd.income_deal_curr) income
FROM im_doc_head idh
,mff_report.stage_complex_deal_head_hist scdhh
,im_complex_deal_detail icdd
,mff_report.v_loc vl
,item_master im
,item_master im_parent
WHERE ( ( idh.type IN ('DEBMEC','CRDMEC') --Debit and Credit Memos in APPROVED or POSTED
AND idh.status IN ('APPRVE','POSTED'))
OR ( idh.type = 'CRDNRC' --Credit Note Requests in APPROVED or MATCHED
AND idh.status IN ('APPRVE','MTCH')))
AND idh.deal_type = 'C'
AND NVL(:PM_supplier,idh.vendor) = idh.vendor
AND idh.doc_date BETWEEN NVL(:PM_doc_date_from,idh.doc_date) AND NVL(:PM_doc_date_to,idh.doc_date)
AND TRUNC(idh.approval_date) BETWEEN NVL(:PM_approval_date_from,TRUNC(idh.approval_date)) AND NVL(:PM_approval_date_to,TRUNC(idh.approval_date))
AND ( ( :PM_edi_812_filter = 'Y'
AND NOT EXISTS (SELECT 1
FROM sups s
,sup_traits_matrix stm
WHERE TO_NUMBER(idh.vendor) = s.supplier_parent
AND s.supplier = stm.supplier
AND stm.sup_trait = 525))
OR :PM_edi_812_filter = 'N'
OR :PM_supplier IS NOT NULL) -- ignore EDI 812 filter if a supplier is selected
AND ( idh.ext_doc_id MEMBER OF mff_report.parse_strings(:PM_ext_doc_id)
OR :PM_ext_doc_id IS NULL)
AND ( :PM_batch_mode = 'N'
OR ( :PM_batch_mode = 'Y' -- batch mode, pick up un-downloaded docs, and exclude matched CNRs which will not be downloaded
AND idh.edi_download_ind = 'N'
AND idh.status != 'MTCH'))
AND idh.deal_id = scdhh.deal_id (+)
AND SUBSTR(idh.ext_doc_id,(INSTR(idh.ext_doc_id,'-',1) + 1),INSTR(idh.ext_doc_id,'-',1,2) - (INSTR(idh.ext_doc_id,'-',1) + 1)) = scdhh.deal_detail_id (+)
AND idh.doc_date = scdhh.end_invoice_date (+)
AND idh.doc_id = icdd.doc_id
AND icdd.location = vl.loc
AND icdd.item = im.item
AND im.item_parent = im_parent.item (+)
AND ( :PM_supplier IS NOT NULL
OR :PM_doc_date_from IS NOT NULL
OR :PM_doc_date_to IS NOT NULL
OR :PM_approval_date_from IS NOT NULL
OR :PM_approval_date_to IS NOT NULL
OR :PM_ext_doc_id IS NOT NULL
OR :PM_batch_mode = 'Y')
GROUP BY idh.vendor
,idh.deal_id
,mff_report.mff_merch_sql.get_sup_name(idh.vendor)
,idh.deal_type
,scdhh.start_invoice_date
,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY')
,idh.doc_date`enter code here`
,TO_CHAR(idh.doc_date,'MM/DD/YYYY')
,idh.ext_doc_id
,idh.custom_doc_ref_2
,idh.custom_doc_ref_3
,idh.custom_doc_ref_4
,idh.total_qty
,idh.total_cost
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.loc_type
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.loc_type
ELSE icdd.loc_type
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.location
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.location
ELSE icdd.location
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN vl.loc_name
WHEN 'P' THEN NULL
WHEN 'PL' THEN vl.loc_name
ELSE vl.loc_name
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item,im.item)
WHEN 'PL' THEN NVL(im_parent.item,im.item)
ELSE im.item
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
WHEN 'PL' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item_desc,im.item_desc)
WHEN 'PL' THEN NVL(im_parent.item_desc,im.item_desc)
ELSE im.item_desc
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'VL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'P' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
WHEN 'PL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
ELSE mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
END
Get the new column order_no from a table not currently in the query
I imagine the answer to this query is related to here, however I am struggling.
Any help is much appreciated.
I am getting the following error message when running a piece of code.
The code works fine in Query form but not in view.
Code
SELECT
CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'A%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'B%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
concat(left(b.Complete,2),a.[CALCULATED_AGE],substring(b.Complete,3,100)) AS [Calculated Age],
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2)) AS [Derived_YearMonth of Death],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2)) AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
c.[Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
a.[LSOA_OF_RESIDENCE_CODE] AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] as [Havering Quintile],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE concat(a.[NHS_ESTABLISHMENT_IND],'-',a.PLACE_OF_DEATH_ESTAB_TYPE)
END
) AS [NHS ESTABLISHMENT CODE],
isnull(f.[Place of Death Establishment Type],a.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS a
inner join REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
left outer JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT
(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e ON { fn CONCAT(ISNULL(a.ULA_OF_RESIDENCE_CODE, ''), ISNULL(a.WARD_OF_RESIDENCE_CODE, '')) } = e.WD14CDO
LEFT OUTER JOIN REFERENCE.[dbo].[GEOG_NHSCE_EstabTypes_2016] AS f
ON
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE {fn concat({ fn CONCAT(ISNUll(convert(nvarchar,a.[NHS_ESTABLISHMENT_IND],21),''),'-')},isnull(convert(nvarchar,a.PLACE_OF_DEATH_ESTAB_TYPE,21),''))}
END = f.[Code]
LEFT OUTER JOIN REFERENCE.[dbo].[OTH_IMD2015] AS g
ON a.[LSOA_OF_RESIDENCE_CODE] = g.[LSOA code (2011)]
order by [Calculated Age]
The code above seems to prefix concatenate function with an {fn as shown below, if that helps anyone to solve this
SELECT
CASE WHEN aPCMD.Sex = 1 THEN 'M'
WHEN aPCMD.Sex = 2 THEN 'F'
ELSE 'unknown'
END AS Derived_Sex,
CASE WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'A%' THEN 'Less than 1'
WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'B%' THEN 'Less than 1'
WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'C%' THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
{ fn CONCAT(LEFT(bAgeLookup.Complete, 2), aPCMD.CALCULATED_AGE, SUBSTRING(bAgeLookup.Complete, 3, 100)) } AS Derived_Age,
YEAR(aPCMD.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(aPCMD.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(aPCMD.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(aPCMD.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
{ fn CONCAT(LEFT(CONVERT(nvarchar, aPCMD.DATE_OF_DEATH, 21), 4), SUBSTRING(CONVERT(nvarchar, aPCMD.DATE_OF_DEATH, 21), 6, 2)) } AS [Derived_YearMonth of Death],
{ fn CONCAT(LEFT(CONVERT(nvarchar, aPCMD.DATE_OF_REGISTRATION, 21), 4), SUBSTRING(CONVERT(nvarchar, aPCMD.DATE_OF_REGISTRATION, 21), 6, 2)) } AS [Derived_YearMonth of Registration],
CASE WHEN LEN(
CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1] WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code], cICD10.[4 character DESCRIPTION] AS Derived,
cICD10.[3 character Sub-Chapter Description],
cICD10.[Chapter Description],
d.District_Name AS [LA NAME],
e.WD14NM AS [WARD NAME],
aPCMD.LSOA_OF_RESIDENCE_CODE AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] AS [Havering Quintile],
(CASE WHEN aPCMD.[NHS_ESTABLISHMENT_IND] = '1' THEN 'NHS'
WHEN aPCMD.[NHS_ESTABLISHMENT_IND] = '2' THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN 'HOME'
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER' END) AS [PLACE OF DEATH CODE],
(CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN ''
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E' THEN ''
ELSE concat(aPCMD.[NHS_ESTABLISHMENT_IND], '-', aPCMD.PLACE_OF_DEATH_ESTAB_TYPE)
END) AS [NHS ESTABLISHMENT CODE],
ISNULL(f.[Place of Death Establishment Type],
aPCMD.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS aPCMD
INNER JOIN REFERENCE.dbo.[ONS-PCMD_AGE] AS bAgeLookup
ON aPCMD.CALCULATED_AGE_UNIT = bAgeLookup.[Age Unit]
LEFT OUTER JOIN REFERENCE.dbo.[OTH_ICD-10] AS cICD10
ON CASE WHEN LEN(
CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT( CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END)
END = cICD10.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON aPCMD.ULA_OF_RESIDENCE_CODE = d.ONS_District_Code
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e
ON { fn CONCAT(ISNULL(aPCMD.ULA_OF_RESIDENCE_CODE, ''), ISNULL(aPCMD.WARD_OF_RESIDENCE_CODE, '')) } = e.WD14CDO
LEFT OUTER JOIN REFERENCE.dbo.GEOG_NHSCE_EstabTypes_2016 AS f
ON CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN ''
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E' THEN ''
ELSE { fn concat({ fn CONCAT(ISNUll(CONVERT(nvarchar, aPCMD.[NHS_ESTABLISHMENT_IND], 21), ''), '-') },
isnull(CONVERT(nvarchar, aPCMD.PLACE_OF_DEATH_ESTAB_TYPE, 21), '')) }
END = f.Code
LEFT OUTER JOIN REFERENCE.dbo.OTH_IMD2015 AS g ON aPCMD.LSOA_OF_RESIDENCE_CODE = g.[LSOA code (2011)]
Try with the below code..
SELECT
CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ ISNULL(a.[CALCULATED_AGE],''))) LIKE 'A%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ ISNULL(a.[CALCULATED_AGE],''))) LIKE 'B%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ISNULL(a.[CALCULATED_AGE],''))) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
left(ISNULL(b.Complete,''),2)+ISNULL(a.[CALCULATED_AGE],'')+substring(ISNULL(b.Complete,''),3,100) AS [Calculated Age],
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
ISNULL(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),'')+
ISNULL(SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2),'') AS [Derived_YearMonth of Death],
ISNULL(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),'')+
ISNULL(SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2),'') AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
ELSE ISNULL(a.UNDERLYING_CAUSE_OF_DEATH,'')
END+ 'X'
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
c.[Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
a.[LSOA_OF_RESIDENCE_CODE] AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] as [Havering Quintile],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE ISNULL(a.[NHS_ESTABLISHMENT_IND],'')+'-'+ISNULL(a.PLACE_OF_DEATH_ESTAB_TYPE,'')
END
) AS [NHS ESTABLISHMENT CODE],
isnull(f.[Place of Death Establishment Type],a.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS a
inner join REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
left outer JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
ELSE ISNULL(a.UNDERLYING_CAUSE_OF_DEATH,'')
END+ 'X'
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e ON ISNULL(a.ULA_OF_RESIDENCE_CODE, '')+ ISNULL(a.WARD_OF_RESIDENCE_CODE, '') = e.WD14CDO
LEFT OUTER JOIN REFERENCE.[dbo].[GEOG_NHSCE_EstabTypes_2016] AS f
ON
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE ISNUll(convert(nvarchar,a.[NHS_ESTABLISHMENT_IND],21),'')+'-'+isnull(convert(nvarchar,a.PLACE_OF_DEATH_ESTAB_TYPE,21),'')
END = f.[Code]
LEFT OUTER JOIN REFERENCE.[dbo].[OTH_IMD2015] AS g
ON a.[LSOA_OF_RESIDENCE_CODE] = g.[LSOA code (2011)]
ORDER BY [Calculated Age]
In a query I do :
SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs END)
AS FACKOSTEN
but I would like to use something like WHEN 'K' or WHEN 'V' THEN
but how do I do that ??
CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs ELSE NULL END
Or
CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs ELSE NULL END
Or
CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
ELSE NULL
END
Or
CASE WHEN kbpres.soort = 'K' THEN kbpres.prijs
WHEN kbpres.soort = 'V' THEN kbpres.prijs
ELSE NULL
END
SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
END)
AS FACKOSTEN
OR
SUM(CASE WHEN kbpres.soort IN('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN
Try this:
SUM(CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN
We don't know wich database engine you use, but in MySQL you can write this :
SUM(CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs END)
AS FACKOSTEN