如何将我的数据从行转换为列?

时间:2014-03-21 12:19:25

标签: sql sql-server

我正在使用sqlserver 2008,我有一个如下数据。行数不确定。对于这个简单的行计数是44,但它可以更多或更少。而且规模总是不变的。

+----------------------------------------------------+-------+
|                       Level                        | Count |
+----------------------------------------------------+-------+
| 0 - 10                                             |    49 |
| 11 - 20                                            |    11 |
| 21 - 30                                            |    15 |
| 31 - 40                                            |    19 |
| 41 - 50                                            |    18 |
| 51 - 60                                            |     9 |
| 61 - 70                                            |     0 |
| 71 - 80                                            |     2 |
| 81 - 90                                            |     2 |
| 91 - 100                                           |     1 |
| 101 - 9999                                         |     9 |
| 0 - 10                                             |    47 |
| 11 - 20                                            |     6 |
| 21 - 30                                            |     5 |
| 31 - 40                                            |     3 |
| 41 - 50                                            |     3 |
| 51 - 60                                            |     5 |
| 61 - 70                                            |     9 |
| 71 - 80                                            |     5 |
| 81 - 90                                            |     8 |
| 91 - 100                                           |    14 |
| 101 - 9999                                         |    30 |
| 0 - 10                                             |    46 |
| 11 - 20                                            |     3 |
| 21 - 30                                            |     4 |
| 31 - 40                                            |     4 |
| 41 - 50                                            |     4 |
| 51 - 60                                            |     1 |
| 61 - 70                                            |     7 |
| 71 - 80                                            |    14 |
| 81 - 90                                            |    13 |
| 91 - 100                                           |    15 |
| 101 - 9999                                         |    24 |
| 0 - 10                                             |    43 |
| 11 - 20                                            |     4 |
| 21 - 30                                            |     3 |
| 31 - 40                                            |     1 |
| 41 - 50                                            |     7 |
| 51 - 60                                            |     3 |
| 61 - 70                                            |     8 |
| 71 - 80                                            |    15 |
| 81 - 90                                            |    13 |
| 91 - 100                                           |    17 |
| 101 - 9999                                         |    21 |
+----------------------------------------------------+-------+

我希望它像下面一样,

+--------+---------+---------+---------+---------+---------+---------+---------+---------+----------+-------+
| 0 - 10 | 11 - 20 | 21 - 30 | 31 - 40 | 41 - 50 | 51 - 60 | 61 - 70 | 71 - 80 | 81 - 90 | 91 - 100 | 101 - |
+--------+---------+---------+---------+---------+---------+---------+---------+---------+----------+-------+
|     49 |      11 |      15 |      19 |      18 |       9 |       0 |       2 |       2 |        1 |     9 |
|     47 |       6 |       5 |       3 |       3 |       5 |       9 |       5 |       8 |       14 |    30 |
|     46 |       3 |       4 |       4 |       4 |       1 |       7 |      14 |      13 |       15 |    24 |
|     43 |       4 |       3 |       1 |       7 |       3 |       8 |      15 |      13 |       17 |    21 |
+--------+---------+---------+---------+---------+---------+---------+---------+---------+----------+-------+

我该怎么做?

3 个答案:

答案 0 :(得分:2)

您可以使用pivot关键字将行条目转换为列。

SELECT  [0 - 10], 
    [11 - 20], 
    [21 - 30], 
    [31 - 40], 
    [41 - 50], 
    [51 - 60], 
    [61 - 70], 
    [71 - 80], 
    [81 - 90], 
    [91 - 100], 
    [101 - 9999] 
FROM (
        SELECT row_number() over(partition by [Level] order by [Count]) as [RowNumber], 
                [Level], 
                [Count] 
            FROM TableName 
     )  as t
            PIVOT (MIN([Count])
                FOR [Level] IN ([0 - 10], [11 - 20], [21 - 30], [31 - 40], [41 - 50], [51 - 60], [61 - 70], [71 - 80], [81 - 90], [91 - 100], [101 - 9999])
                ) as p

答案 1 :(得分:0)

SELECT MAX(CASE WHEN LEVEL='0 - 10' THEN Count ELSE 0 END) as '0 - 10',
       MAX(CASE WHEN LEVEL='11 - 20' THEN Count ELSE 0 END) as '11 - 20',
....
FROM table
GROUP BY LEVEL

答案 2 :(得分:0)

我认为以下查询会对您有所帮助。

[LevelTable]是您的表,列是[Level]和[Count]

SELECT [0 - 10], [11 - 20]
FROM
(
  SELECT B.[Level], A.[Count]
    , row_number() over(partition by a.[Level]
                        order by a.[Count]) seq
  FROM [LevelTable] AS A 
  INNER JOIN (select distinct [Level] from [LevelTable]) AS B 
    ON A.[Level] = B.[Level]
) AS P
PIVOT
(
  min(P.[Count])
  for P.[Level]  in ([0 - 10], [11 - 20])
) AS PIV;

已添加两个级别I的示例。您可以修改所有级别的查询。