问题
I have a pivot query which works well until now, but there has been a requirement where I need to specify a condition in the unpivot query which would omit the rows and column values which are not matching with the condition and displays me null. However, I need to show those value as a part of final results.
I have tried to do union to include the missing values, but won't get the desired output. I am pasting my pivot query below along with the results which are displayed and which needs to be displayed as a result. Please help.
CURRENT QUERY
SELECT
OrderID, AccessName, Address1, Postcode
, Gen_Instr_1 AS "General Instructions for Install"
,Supplier_Name_1 AS "Supplier Name"
,Install_Job_Name_1 AS "Install Jobs"
,Install_Job_Status_1 AS "Install Job Status"
,Install_Job_Name_2 AS "EPR Jobs"
,Install_Job_Status_2 AS "EPR Job Status"
FROM
(
SELECT
OrderID, AccessName, Address1, Postcode
, col+'_'+CAST(rn AS VARCHAR(10)) col,
val
FROM
(
SELECT o.OrderID,
CAST(js.JobStatusID AS VARCHAR(50)) JobStatusId
, CAST(p.Name AS VARCHAR(50)) Install_Job_Name
, o.AccessName, o.Address1, o.Postcode
, CAST(oj.GeneralInstructions AS VARCHAR(50)) Gen_Instr
, CAST(s.CompanyName AS VARCHAR(50)) Supplier_Name
, oj.SupplierID
, CAST(js.Name AS VARCHAR(50)) Install_Job_Status
, ROW_NUMBER() OVER(PARTITION BY o.OrderID ORDER BY o.OrderID) rn
FROM
NEPCCO.Orders o
INNER JOIN NEPCCO.Clients c ON o.ClientID = c.ClientID
INNER JOIN NEPCCO.OrderJobs oj ON o.OrderID = oj.OrderID
INNER JOIN NEPCCO.Suppliers s ON oj.SupplierID = s.SupplierID
INNER JOIN NEPCCO.Products p ON oj.ProductID = p.ProductID
INNER JOIN NEPCCO.OrderStatus os ON o.OrderStatusID = os.OrderStatusID
INNER JOIN NEPCCO.JobStatus js ON oj.JobStatusID = js.JobStatusID
WHERE
o.OrderID IN (6981,6860,6982,6983) AND
(p.ProductID IN (35,36,37,38,38,40,41,42,43) OR p.ProductID IN (33,34))
AND s.CompanyName = 'Northern Gas Heating Ltd'
) d
UNPIVOT
(
val
FOR col IN (JobStatusId, Gen_Instr, Supplier_Name,Install_Job_Name, Install_Job_Status)
) un
) s
PIVOT
(
MAX(val)
FOR col IN (JobStatusID_1, Gen_Instr_1, Supplier_Name_1, Install_Job_Name_1, Install_Job_Status_1,
JobStatusID_2, Gen_Instr_2,
Install_Job_Name_2, Install_Job_Status_2
)
) piv
CURRENT RESULTS - SEE THE NULL VALUES IN LAST TWO COLUMNS
OrderID AccessName Address1 Postcode SupplierName Install Jobs Install Job Status EPR Jobs EPR Job Status
6981 Mrs Cespedes 73 Mill Lane WV11 1DQ Northern Gas Heating Ltd GC1 - 28 Complete NULL NULL
6983 Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas Heating Ltd GC1 - 28 Complete NULL NULL
Now, if I runn the inner unpivot query as below, I get following records/rows
Inner unpivot query
SELECT o.OrderID,
CAST(js.JobStatusID AS VARCHAR(50)) JobStatusId
, CAST(p.Name AS VARCHAR(50)) Install_Job_Name
, o.AccessName, o.Address1, o.Postcode
, CAST(oj.GeneralInstructions AS VARCHAR(50)) Gen_Instr
, CAST(s.CompanyName AS VARCHAR(50)) Supplier_Name
, oj.SupplierID
, CAST(js.Name AS VARCHAR(50)) Install_Job_Status
, ROW_NUMBER() OVER(PARTITION BY o.OrderID ORDER BY o.OrderID) rn1
FROM
NEPCCO.Orders o
INNER JOIN NEPCCO.Clients c ON o.ClientID = c.ClientID
INNER JOIN NEPCCO.OrderJobs oj ON o.OrderID = oj.OrderID
INNER JOIN NEPCCO.Suppliers s ON oj.SupplierID = s.SupplierID
INNER JOIN NEPCCO.Products p ON oj.ProductID = p.ProductID
INNER JOIN NEPCCO.OrderStatus os ON o.OrderStatusID = os.OrderStatusID
INNER JOIN NEPCCO.JobStatus js ON oj.JobStatusID = js.JobStatusID
WHERE
o.OrderID IN (6981,6860,6982,6983) AND
(p.ProductID IN (35,36,37,38,38,40,41,42,43) OR p.ProductID IN (33,34))
that produces following results
OrderID AccessName Address1 Postcode SupplierName Install Jobs Install Job Status EPR Jobs EPR Job Status
6860 6 AW EPR 01625555555 1 Gorsey Road SK9 5DU OGP 14 Cancelled 1
6981 4 AW EPR Mrs Cespedes 73 Mill Lane WV11 1DQ Ian Barnhurst 1 Complete 1
6981 4 GC1 - 28 Mrs Cespedes 73 Mill Lane WV11 1DQ Northern Gas Heating Ltd 403 Complete 2
6982 4 AW EPR Installer Mr N Singh 115 Oxbarn Avenue WV3 7HQ Northern Gas 414 Complete 1
6983 4 AW EPR Installer Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas 414 Complete 1
6983 4 GC1 - 28 Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas Heating Ltd 403 Complete 2
Problem
Now, if you see carefully the above result set, which has the order id's 6860, 6982
, which is not included as a part of above result because it hasn't got the supplier name as Northern Gas Heating Ltd
, moreover, the results which are displayed on first query, omits the values from order id's 6981, 6983
, also, because they have different suppliers.
Also, if you notice, with the records being displayed in first query, the values with row number 2 are not included.
I tried my best but failed. @bluefeet, your expertise highly required over here. Any clarifications please ask.
来源:https://stackoverflow.com/questions/17366351/pivot-query-missing-records