SQL数据透视表结合多行

时间:2015-10-19 22:01:24

标签: sql-server pivot-table

我有以下数据

+----+-----------+---------+------+------------+-------------+------------+
| id | selection | against | runs | firstplace | secondplace | thirdplace |
+----+-----------+---------+------+------------+-------------+------------+
|  1 | a         | a       |    0 |          0 |           0 |          0 |
|  2 | a         | b       |   20 |          0 |           3 |          0 |
|  3 | a         | c       |   10 |          0 |           3 |          0 |
|  4 | a         | d       |   19 |          0 |           3 |          0 |
|  5 | b         | a       |   20 |          2 |           4 |          1 |
|  6 | b         | b       |    0 |          0 |           0 |          0 |
|  7 | b         | c       |   14 |          2 |           4 |          1 |
|  8 | b         | d       |   23 |          2 |           4 |          1 |
|  9 | c         | a       |   10 |          0 |           0 |          0 |
| 10 | c         | b       |   14 |          0 |           0 |          0 |
| 11 | c         | c       |    0 |          0 |           0 |          0 |
| 12 | c         | d       |   13 |          0 |           0 |          0 |
| 13 | d         | a       |   19 |          1 |           2 |          1 |
| 14 | d         | b       |   23 |          1 |           2 |          1 |
| 15 | d         | c       |   13 |          1 |           2 |          1 |
| 16 | d         | d       |    0 |          0 |           0 |          0 |
+----+-----------+---------+------+------------+-------------+------------+

据我所知,我需要创建一个数据透视查询,以便将数据作为

返回
+---+------------+-------------+------------+------------+
|   |     a      |      b      |      c     |      d     |
+---+------------+-------------+------------+------------+
| a | 0  [0/0/0] | 20 [0/2/0]  | 10 [0/3/0] | 19 [0/3/0] |
| b | 20 [2/4/1] | 0  [0/0/0]  | 14 [2/4/1] | 23 [2/4/1] |
| c | 10 [0/0/0] | 14 [0/0/0]  | 0  [0/0/0] | 13 [0/0/0] |
| d | 19 [1/2/1] | 23 [13/2/1] | 13 [1/2/1] | 0  [0/0/0] |
+---+------------+-------------+------------+------------+

运行格式,然后运行[]

中的第一,第二和第三数据

根据行选择列。

我已经查看了有关数据透视表的所有示例,但它们都有一个计算,似乎只有2或3列。

我是否以正确的方式进行此操作,即数据透视表或我是否需要查看其他内容?

非常感谢。

2 个答案:

答案 0 :(得分:1)

如果您有四个组,那么您可以使用pivot或条件聚合编写查询以进行透视:

select selection,
       max(case when against = 'a' then val end) as a,
       max(case when against = 'b' then val end) as b,
       max(case when against = 'c' then val end) as c,
       max(case when against = 'd' then val end) as d
from (select t.*,
             replace(replace(replace(replace('@1 [@2/@3/@4]', @1, runs
                                            ), @2, firstplace
                                    ), @3, secondplace
                            ), @4, thirdplace
                    ) as val
      from t
     ) t
group by selection;

如果你不了解这些群组,那么你需要一个动态的角色。我建议您使用Google“SQL Server动态数据透视”来获得解决该问题的一些方法。

答案 1 :(得分:0)

与戈登更优雅的方法略有不同。

SELECT selection, 
max(case when against = 'a' then
  runs + " ["+ firstPlace +"/"+secondPlace+"/"+thirdPlace+"]" end) as "A",
max(case when against = 'b' then
  runs + " ["+ firstPlace +"/"+secondPlace+"/"+thirdPlace+"]" end) as "B",
max(case when against = 'c' then
  runs + " ["+ firstPlace +"/"+secondPlace+"/"+thirdPlace+"]" end) as "C",
max(case when against = 'D' then
  runs + " ["+ firstPlace +"/"+secondPlace+"/"+thirdPlace+"]" end) as "D",
FROM tableName
GROUP BY selection