使用sum将SQL表行转换为3列

时间:2016-02-21 07:14:58

标签: sql sql-server sql-server-2008 sql-server-2012

我尝试透视我的{{​​1}}表,但我无法获得所需的结果 我的表看起来像这样

SQL

我如何需要Pivot查看

DOEntry        RouteID      StoreID        Status
----------------------------------------------------

01-02-2016      100           1134          Complete
01-02-2016      100           419          No Visit
01-02-2016      100           420          Complete
01-02-2016      100           423          Complete
01-02-2016      100           451          Complete
01-02-2016      100           457          Complete
01-02-2016      100           488          Complete
01-02-2016      100           667          Complete
01-02-2016      101           373          Complete
01-02-2016      101          374           Complete
01-02-2016      101          376           Complete
01-02-2016      101          427           Complete
01-02-2016      101          460           Complete
01-02-2016      101          465           Complete
01-02-2016      101          616           No Visit
01-02-2016      101          688           No Visit
02-02-2016      100          421           No Visit
02-02-2016      100          422           No Visit
02-02-2016      100          424           Complete
02-02-2016      100          425           No Visit
02-02-2016      100          456           No Visit
02-02-2016      100          458           Complete
02-02-2016      100          459           Complete
02-02-2016      100          486           Complete
02-02-2016      101          375           Complete
02-02-2016      101          377           Complete
02-02-2016      101          378           Complete
02-02-2016      101          383           No Visit
02-02-2016      101          426           Complete
02-02-2016      101          604           Complete
02-02-2016      101          605           No Visit
02-02-2016      101          609           Complete

DOEntry RouteID [Complete] [InComplete] [No Visit] 01-02-2016 100 7 0 1 01-02-2016 101 6 0 2 02-02-2016 100 4 0 4 02-02-2016 101 6 0 2 Complete[InComplete][No Visit]

中的总和

仅供参考:在给定的情况下,我确实理解您没有看到StoreID,但确实存在大规模InComplete
感谢

1 个答案:

答案 0 :(得分:2)

考虑STATUS列中的值的数量是固定且已知的。如果状态中的值未固定且未知,则需要使用Dynamic Sql进行旋转

使用Conditional Aggregate执行此操作

SELECT DOEntry,
       RouteID,
       [Complete]= Sum(Iif(status = 'Complete', 1, 0)),
       [InComplete]= Sum(Iif(status = 'InComplete', 1, 0)),
       [No Visit]= Sum(Iif(status = 'No Visit', 1, 0))
FROM   Yourtable
GROUP  BY DOEntry,
          RouteID 
Order by DOEntry

或您可以使用PIVOT运算符

SELECT DOEntry,
       RouteID,
       [Complete],
       [InComplete],
       [No Visit]
FROM   Yourtable
       PIVOT (Count(StoreID)
             FOR Status IN ([Complete],
                            [InComplete],
                            [No Visit]))pv
ORDER  BY DOEntry 

结果:

╔════════════╦═════════╦══════════╦════════════╦══════════╗
║  DOEntry   ║ RouteID ║ Complete ║ InComplete ║ No Visit ║
╠════════════╬═════════╬══════════╬════════════╬══════════╣
║ 2016-01-02 ║     100 ║        7 ║          0 ║        1 ║
║ 2016-01-02 ║     101 ║        6 ║          0 ║        2 ║
║ 2016-02-02 ║     100 ║        4 ║          0 ║        4 ║
║ 2016-02-02 ║     101 ║        6 ║          0 ║        2 ║
╚════════════╩═════════╩══════════╩════════════╩══════════╝

两者都有类似的执行计划,你可以使用其中任何一个

Conditonal Aggregate

OneDrive Invite people to folder img

透视

Conditonal Aggregate