在不同列的总和上创建一个轴

时间:2012-12-13 20:16:26

标签: sql sql-server sql-server-2008 tsql pivot

我的表格如下:

+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+----------+-------------+
| client_id | count1 | count2 | count3 | count4 | count5 | count6 | count7 | count8 | count9 | count10 | count11 | count12 | Count13+ | PatientYear |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+----------+-------------+
|       123 |     23 |      8 | NULL   | NULL   | NULL   | NULL   | 1      | 1      | NULL   | NULL    | NULL    | NULL    | NULL     |    20120101 |
|       234 |     52 |     39 | 18     | 12     | 14     | 17     | 17     | 13     | 14     | 12      | 10      | 3       | 27       |    20120101 |
|        34 |     25 |      5 | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL    | NULL    | NULL     |    20120101 |
|       345 |    211 |     29 | 31     | 9      | 7      | 4      | 6      | NULL   | NULL   | 1       | NULL    | 1       | 4        |    20120101 |
|      3457 |     13 |      4 | 3      | 7      | 8      | 6      | 9      | 1      | 7      | 5       | NULL    | 1       | 9        |    20120101 |
|      5645 |     39 |     15 | 14     | 7      | 13     | 4      | 14     | 6      | 3      | 5       | 2       | 1       | 41       |    20120101 |
|       474 |     47 |     25 | 12     | 8      | 6      | 3      | 5      | 6      | 6      | 3       | 1       | 8       | 26       |    20120101 |
|        66 |     11 |      3 | 2      | 1      | 2      | NULL   | 1      | 1      | NULL   | 2       | 1       | 1       | 18       |    20120101 |
|       333 |      5 |      3 | 1      | 1      | 2      | 3      | NULL   | 1      | 2      | 1       | NULL    | NULL    | 28       |    20120101 |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+----------+-------------+

...

我正在尝试创建这样的支点:

+-----------------+----------+----------+----------+----------+----------+
|     Values      | 20080101 | 20090101 | 20100101 | 20110101 | 20120101 |
+-----------------+----------+----------+----------+----------+----------+
| Sum of count1   |    20895 |    87470 |   213877 |   398484 |   533062 |
| Sum of count2   |     2750 |    19020 |    48820 |    95423 |   142200 |
| Sum of count3   |      477 |     5958 |    19419 |    38449 |    61968 |
| Sum of count4   |      201 |     2012 |     8865 |    19098 |    30179 |
| Sum of count5   |       71 |      911 |     3894 |     8941 |    15675 |
| Sum of count6   |       39 |      612 |     2148 |     5380 |     9899 |
| Sum of count7   |       14 |      427 |     1466 |     3555 |     6450 |
| Sum of count8   |       11 |      239 |     1036 |     2381 |     4667 |
| Sum of count9   |        2 |      137 |      726 |     1888 |     4000 |
| Sum of count10  |        2 |       82 |      544 |     1399 |     3600 |
| Sum of count11  |        1 |       53 |      370 |     1094 |     3122 |
| Sum of count12  |        4 |       38 |      262 |      826 |     1845 |
| Sum of Count13+ |       32 |      188 |      609 |     2662 |     4044 |
+-----------------+----------+----------+----------+----------+----------+

这是我到目前为止所尝试的内容:

select
* from 
( 
    select *
    from #TempTable
) DataTable
PIVOT
(
    sum (count1)
    ,sum(count2)
    For PatientYear
    in ([20120101],[20110101],[20100101],[20090101],[20080101])

)PivotTable

但是我收到了这个错误:

  

Msg 102,Level 15,State 1,Line 11','。附近的语法不正确。

我做错了什么?感谢您的指导。

1 个答案:

答案 0 :(得分:1)

您需要先UNPIVOT数据,然后应用PIVOT

select col, [20120101],[20110101], [20100101],[20090101],[20080101]
from
(
  select PatientYear,
    value, col,
    cast(replace(replace(col, '+', ''), 'count', '') as int) col_ord
  from
  (
    -- if you have different datatypes, then you will need to perform a cast/convert here in this query
    select [client_id], [count1], [count2], [count3], [count4], [count5], [count6], [count7], 
           [count8], [count9], [count10], [count11], [count12], [Count13+], [PatientYear]
    from yourtable
  ) s1
  unpivot
  (
    value
    for col in ([count1], [count2], [count3],      
                [count4], [count5], [count6], 
                [count7], [count8], [count9], 
                [count10], [count11], [count12], [Count13+])
  ) unpiv
) src
pivot
(
  sum(value)
  for PatientYear in ([20120101],[20110101],
                      [20100101],[20090101],[20080101])
) piv
order by col_ord

请参阅SQL Fiddle with Demo