2

我不知道这是否会被正式称为枢轴,但我想要的结果是:

+------+---------+------+
| Alex | Charley | Liza |
+------+---------+------+
|  213 |     345 |    1 |
|   23 |     111 |    5 |
|   42 |      52 |    2 |
|  323 |         |    5 |
|   23 |         |    1 |
|  324 |         |    5 |
+------+---------+------+

我的输入数据是这种形式:

+-----+---------+
| Apt |  Name   |
+-----+---------+
| 213 | Alex    |
|  23 | Alex    |
|  42 | Alex    |
| 323 | Alex    |
|  23 | Alex    |
| 324 | Alex    |
| 345 | Charley |
| 111 | Charley |
|  52 | Charley |
|   1 | Liza    |
|   5 | Liza    |
|   2 | Liza    |
|   5 | Liza    |
|   1 | Liza    |
|   5 | Liza    |
+-----+---------+

因为我有大约 100 个名字,我不想像这样做大量的子查询

select null, null, thirdcolumn from...
select null, seconcolumn from...
select firstcolumn from...

有没有办法用PIVOT或其他方式做到这一点?

4

2 回答 2

2

You can do this with dynamic PIVOT and the ROW_NUMBER() function:

DECLARE @cols AS VARCHAR(1000),
        @query  AS VARCHAR(8000)
SELECT @cols = STUFF((SELECT ',' +   QUOTENAME(Name) 
                    FROM (SELECT DISTINCT Name
                          FROM #test
                          )sub
                    ORDER BY Name
                    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)') 
                    ,1,1,'')
PRINT @cols

SET @query = '
WITH cte AS (SELECT DISTINCT *
             FROM  #test)
    ,cte2 AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Apt)RowRank
             FROM  cte)
SELECT * 
FROM  cte2 
PIVOT (max(Apt) for Name in ('+@cols+')) p
            '
EXEC (@query)

SQL Fiddle - Distinct List, Specific Order

Edit: If you don't want the list to be distinct, eliminate the first cte above, and if you want to keep arbitrary ordering change the ORDER BY to (SELECT 1):

DECLARE @cols AS VARCHAR(1000),
        @query  AS VARCHAR(8000)
SELECT @cols = STUFF((SELECT ',' +   QUOTENAME(Name) 
                    FROM (SELECT DISTINCT Name
                          FROM #test
                          )sub
                          ORDER BY Name
                    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)') 
                    ,1,1,'')
PRINT @cols

SET @query = '
WITH cte AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY Name ORDER BY (SELECT 1))RowRank
             FROM  #test)
SELECT * 
FROM  cte 
PIVOT (max(Apt) for Name in ('+@cols+')) p
            '
EXEC (@query)

SQL Fiddle - Full List, Arbitrary Order

And finally, if you didn't want the RowRank field in your results, just re-use the @cols variable in your SELECT:

SET @query = '
WITH cte AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY Name ORDER BY (SELECT 1))RowRank
             FROM  #test)
SELECT '+@cols+' 
FROM  cte 
PIVOT (max(Apt) for Name in ('+@cols+')) p
            '
EXEC (@query)
于 2013-08-16T22:16:04.703 回答
2

哦,这很痛苦,但是您可以使用 SQL 来完成。您正在尝试连接列。

select seqnum,
       max(case when name = 'Alex' then apt end) as Alex,
       max(case when name = 'Charley' then apt end) as Charley,
       max(case when name = 'Liza' then apt end) as Liza
from (select t.*, row_number() over (partition by name order by (select NULL)) as seqnum
      from t
     ) t
group by seqnum
order by seqnum;

注意:不能保证每列中的原始顺序相同。如您所知,SQL 表本质上是无序的,因此您需要一个列来指定排序。

要处理多个名称,我只需使用以下查询获取列表:

select distinct 'max(case when name = '''+name+''' then apt end) as '+name+','
from t;

并将结果复制到查询中。

于 2013-08-16T22:18:16.177 回答