【问题标题】:How to make two pivot column with same column name using SQL Server?如何使用 SQL Server 创建两个具有相同列名的数据透视列?
【发布时间】:2017-08-10 06:38:24
【问题描述】:

我想使用具有相同列的聚合函数创建两个数据透视列。

ItemLookupCode    StoreID    DepartmentID    Weeks        QtySold   AsOfWeekOnHand
----------------------------------------------------------------------------------
610759C2000        1001        23             30             0         1.5
610759C2000        1001        23             31             0          0
610759C2000        1004        23             30             0          2
610759C2000        1004        23             31             0         3.5 
610759C2000        1201        23             30           0.6395       1
610759C2000        1201        23             31           0.6395       2

我尝试使用以下查询。但这是错误的。正确的方法是什么?

select itemlookupcode, storeid, departmentid,[30],[31] from 
(
    select 
        fr.itemlookupcode,
        fr.storeid,
        fr.departmentid,
        fr.asofweekonhand,
        fr.weeks,
        fr.QtySold
    from 
        #finalresult fr
) x
pivot 
(
    sum(QtySold)
    for weeks in ([30],[31])
) p1 
pivot 
(
    sum(asofweekonhand)
    for weeks in ([30],[31])
) p2 

注意

我们可以将列名指定为

Week30Sold    Week31Sold    Week30AsOfWeekOnHand    Week31AsOfWeekOnHand
-------------------------------------------------------------------------

【问题讨论】:

    标签: sql sql-server stored-procedures pivot


    【解决方案1】:

    我认为这大致符合您的要求:

    declare @t table (ItemLookupCode varchar(20), StoreID int, DepartmentID int, Weeks int,
                      QtySold decimal(10,4), AsOfWeekOnHand decimal(10,4))
    insert into @t(ItemLookupCode,StoreID,DepartmentID,Weeks,QtySold,AsOfWeekOnHand) values
    ('610759C2000',1001,23,30,  0   ,1.5 ),
    ('610759C2000',1001,23,31,  0   , 0  ),
    ('610759C2000',1004,23,30,  0   , 2  ),
    ('610759C2000',1004,23,31,  0   ,3.5 ),
    ('610759C2000',1201,23,30,0.6395, 1  ),
    ('610759C2000',1201,23,31,0.6395, 2  )
    
    select
        *
    from
        (select ItemLookupCode,StoreID,DepartmentID,
          CONVERT(varchar(13),Weeks) + 'Qty' as Weeks,
          QtySold from @t) t1
        pivot (SUM(QtySold) for Weeks in ([30Qty],[31Qty])) p1
        cross apply
        (select CONVERT(varchar(13),Weeks) + 'AsOf' as Weeks,AsOfWeekOnHand
        from @t t2
        where t2.ItemLookupCode = p1.ItemLookupCode and
        t2.DepartmentID = p1.DepartmentID and
        t2.StoreID = p1.StoreID) t2
        pivot (SUM(AsOfWeekOnHand) for Weeks in ([30AsOf],[31AsOf])) p2
    

    结果:

    ItemLookupCode       StoreID     DepartmentID 30Qty      31Qty    30AsOf   31AsOf
    -------------------- ----------- ------------ ---------- -------- -------- -------
    610759C2000          1001        23           0.0000     0.0000   1.5000   0.0000
    610759C2000          1004        23           0.0000     0.0000   2.0000   3.5000
    610759C2000          1201        23           0.6395     0.6395   1.0000   2.0000
    

    注意:

    • 您不能使用相同的列进行两次透视 - 在透视之后,透视子句第一部分中提到的列不再存在,已被括号内的新列名替换。

    • 我们必须对子查询执行apply 而不是JOIN 以避免引入重复 列(例如,如果@987654327,ItemLookupCode 将在结果集中出现两次@ 是子查询的 join

    • 我借此机会重命名了子查询中的Weeks

    • 当我们使用 APPLY 时,我们必须使用 p1 作为外部引用 - PIVOT 会生成一个全新的结果集,它会替换任何现有的结果集/别名。

      李>
    • 正如my answer to your earlier question 中提到的,PIVOT 实际上是GROUP BYs 所有未在PIVOT 子句中提及的列 - 那么为什么第一个 PIVOT 生成的列不是在第二个支点?因为我们已经知道ItemLookupCodeStoreIDDepartmentID 的每个组合本身都是唯一的,因为第一个PIVOT

    【讨论】:

    • 您能否建议我在任何教程页面中阅读有关枢轴的更多信息以了解有关枢轴的更多信息?..
    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2013-07-24
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多