透视查询 - 缺少记录

时间:2013-06-28 13:54:53

标签: sql-server pivot unpivot

我有一个直到现在才运行的数据透视查询,但是我需要在unpivot查询中指定一个条件,该条件会省略与条件不匹配的行和列值并显示null 。但是,我需要将这些值显示为最终结果的一部分。

我曾尝试使用union来包含缺失值,但无法获得所需的输出。我正在粘贴下面的数据透视查询以及显示的结果以及需要显示的结果。请帮忙。

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

当前结果 - 在最后两列中看到空值

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查询,我会得到以下记录/行

内部univot查询

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为6860, 6982,但未包含在上述结果中,因为它没有将供应商名称设为Northern Gas Heating Ltd此外,第一个查询显示的结果省略了订单ID 6981, 6983的值,因为它们有不同的供应商。

另外,如果您注意到,在第一次查询中显示记录时,不包括行号为2的值。

我尽我所能但失败了。 @bluefeet,您的专业知识在这里非常需要。任何澄清请问。

0 个答案:

没有答案