SQLServer-使用组透视表

问题描述 投票:0回答:1

我想知道我想做的事是否可行。我相信它在TSQL中使用PIVOT函数,但对PIVOT函数的经验不足,无法知道从哪里开始。

基本上,我试图获取名为#tmpbudgetdata的以下#表(为简单起见,将其截断):

Account              Description                                                                                          BudgetAmount          Period
-------------------- ---------------------------------------------------------------------------------------------------- --------------------- --------------------
4001                 Mood Embedded Account                                                                                0.00                  1
4001                 Mood Embedded Account                                                                                0.00                  2
4001                 Mood Embedded Account                                                                                0.00                  3
4001                 Mood Embedded Account                                                                                0.00                  4
4001                 Mood Embedded Account                                                                                0.00                  5
4001                 Mood Embedded Account                                                                                0.00                  6
4001                 Mood Embedded Account                                                                                0.00                  7
4001                 Mood Embedded Account                                                                                0.00                  8
4001                 Mood Embedded Account                                                                                0.00                  9
4001                 Mood Embedded Account                                                                                0.00                  10
4001                 Mood Embedded Account                                                                                0.00                  11
4001                 Mood Embedded Account                                                                                0.00                  12
4003                 DBS Music                                                                                            0.00                  1
4003                 DBS Music                                                                                            0.00                  2
4003                 DBS Music                                                                                            0.00                  3
4003                 DBS Music                                                                                            0.00                  4
4003                 DBS Music                                                                                            0.00                  5
4003                 DBS Music                                                                                            0.00                  6
4003                 DBS Music                                                                                            0.00                  7
4003                 DBS Music                                                                                            0.00                  8
4003                 DBS Music                                                                                            0.00                  9
4003                 DBS Music                                                                                            0.00                  10
4003                 DBS Music                                                                                            0.00                  11
4003                 DBS Music                                                                                            0.00                  12
4010                 Sales - Software                                                                                     5040.00               1
4010                 Sales - Software                                                                                     0.00                  2
4010                 Sales - Software                                                                                     6280.56               3
4010                 Sales - Software                                                                                     6947.93               4
4010                 Sales - Software                                                                                     4800.00               5
4010                 Sales - Software                                                                                     0.00                  6
4010                 Sales - Software                                                                                     2400.00               7
4010                 Sales - Software                                                                                     2550.00               8
4010                 Sales - Software                                                                                     4800.00               9
4010                 Sales - Software                                                                                     2400.00               10
4010                 Sales - Software                                                                                     0.00                  11
4010                 Sales - Software                                                                                     2400.00               12
4015                 New Install Revenue                                                                                  0.00                  1
4015                 New Install Revenue                                                                                  0.00                  2
4015                 New Install Revenue                                                                                  0.00                  3
4015                 New Install Revenue                                                                                  3844.79               4
4015                 New Install Revenue                                                                                  0.00                  5
4015                 New Install Revenue                                                                                  0.00                  6
4015                 New Install Revenue                                                                                  0.00                  7
4015                 New Install Revenue                                                                                  0.00                  8
4015                 New Install Revenue                                                                                  0.00                  9
4015                 New Install Revenue                                                                                  0.00                  10
4015                 New Install Revenue                                                                                  0.00                  11
4015                 New Install Revenue                                                                                  0.00                  12

并将其变成类似这样的内容:

Account Description      Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8 Period9 Period10 Period11 Period12
------- --------------- -------- ------- -------- ------ ------- ------- -------- ------ ------- -------- -------- --------
4001   Mood Enabled...  0.00     0.00    0.00    0.00    0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00
4003   Dbs Music        0.00     0.00    0.00    0.00    0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00 
4010   Sales - Software 5040.00  0.00    6280.56 6947.93 4800.00  0.00   2400.00 2550.00  4800.00 2400.00 0.00    2400.00
...etc...

基本上只是通过“帐户”列进行分组(每个帐户的描述相同),然后获取期间值并水平旋转它们。

我知道我可以用游标完成并循环浏览,但是想知道是否可以通过枢轴或其他方式实现。

提前感谢

sql sql-server tsql
1个回答
0
投票

我简单的PIVOT应该可以解决问题

示例

Select *
 From  (
        Select [Account] 
              ,[Description]
              ,Period = concat('Period',Period)
              ,[BudgetAmount]
          From YourTable
       ) src 
 Pivot (sum([BudgetAmount]) for Period in ( [Period1],[Period2],[Period3],[Period4],[Period5],[Period6],[Period7],[Period8],[Period9],[Period10],[Period11],[Period12] ) ) pvt

返回

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.