我有一个到目前为止运行良好的数据透视查询,但是有一个要求,我需要在 unpivot 查询中指定一个条件,该条件将省略与条件不匹配的行和列值并显示为空。但是,我需要将这些值显示为最终结果的一部分。
我试图进行联合以包含缺失值,但不会获得所需的输出。我在下面粘贴我的数据透视查询以及显示的结果和需要显示的结果。请帮忙。
当前查询
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
当前结果 - 查看最后两列中的空值
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
现在,如果我如下运行内部 unpivot 查询,我会得到以下记录/行
内部反透视查询
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))
产生以下结果
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
问题
现在,如果您仔细查看上面的结果集,其中包含订单 id's 6860, 6982
,它没有包含在上述结果中,因为它没有将供应商名称作为Northern Gas Heating Ltd
,此外,在第一次查询时显示的结果,省略来自订单 id 的值6981, 6983
,也是因为它们有不同的供应商。
此外,如果您注意到,在第一个查询中显示记录时,不包括行号为 2 的值。
我尽力了,但失败了。@bluefeet,这里非常需要您的专业知识。任何澄清请询问。