T-SQL PIVOT在一列上附加其他列

时间:2016-02-25 12:37:11

标签: sql sql-server tsql pivot

鉴于此表(总共有两个重复的组合):

Combination Variable    Value
-----------------------------
0           a           1
0           b           2
1           c           3
1           d           4
2           e           5
2           f           6
...

我想查询它以获取此信息:

Variable 0  Value 0     Variable 1  Value 1     Variable 2  Value 2     ...
---------------------------------------------------------------------------
a           1           c           3           e           5
b           2           d           4           f           6

我尝试过使用PIVOT进行动态查询,但无法得到令人满意的结果。

有人可以建议吗?

编辑:虽然Ullas解决方案适用于组合对,但我想知道是否可以使用组合N-uplets(例如(0,0,0),(1,1,1),(2)实现相同的结果,2,2)应该导致3行)?我认为动态查询仍然是要走的路,也许这次是PIVOT。

1 个答案:

答案 0 :(得分:6)

使用动态sql 我刚刚创建了一个。不知道它有多高效。

<强>查询

declare @query1 varchar(max);
declare @query2 varchar(max);

select @query1 = 'select ' + 
 STUFF
 (
   (
        select distinct 
        ',min(t.Variable' + cast(Combination as varchar(6)) + ') as Variable' + 
        cast(Combination as varchar(6))  +
        ',min(t.Value' + cast(Combination as varchar(6)) + ') as Value' + 
        cast(Combination as varchar(6)) 
          from tblComb 
          for xml path('')
      ),
     1,1,'');
select @query1 += ' from('
select @query1 += 'select '+
 stuff
 (
    (
        select distinct 
        ',max(case when Combination = ' + cast(Combination as varchar(6)) +' 
        then Variable end) as Variable' + cast(Combination as varchar(6)) + 
        ',max(case when Combination = ' + cast(Combination as varchar(6)) +' 
        then Value end) as Value' + cast(Combination as varchar(6))
        from tblComb
        for xml path('')
        ), 
        1, 1, '');

select @query1 += ' from tblComb group by Combination, Variable)t union all ';

select @query2 = 'select ' + 
 STUFF
  (
    (
        select distinct 
        ',max(t.Variable' + cast(Combination as varchar(6)) + ') as Variable' + 
         cast(Combination as varchar(6))  +
        ',max(t.Value' + cast(Combination as varchar(6)) + ') as Value' + 
         cast(Combination as varchar(6)) 
         from tblComb 
         for xml path('')
         ), 
         1, 1, '');
select @query2 += ' from('
select @query2 += 'select '+
 stuff
  (
    (
        select distinct 
        ',max(case when Combination = ' + cast(Combination as varchar(6)) +' 
        then Variable end) as Variable' + cast(Combination as varchar(6)) + 
        ',max(case when Combination = ' + cast(Combination as varchar(6)) +' 
        then Value end) as Value' + cast(Combination as varchar(6))
        from tblComb
        for xml path('')
        ), 
        1, 1, '');

select @query2 += ' from tblComb group by Combination, Variable)t;';
select @query1 += @query2;                                      
execute(@query1);

示例表

+-------------+----------+-------+
| Combination | Variable | Value |
+-------------+----------+-------+
| 0           | a        | 1     |
| 0           | b        | 2     |
| 1           | c        | 3     |
| 1           | d        | 4     |
| 2           | e        | 5     |
| 2           | f        | 6     |
+-------------+----------+-------+

结果集

+-----------+--------+-----------+--------+-----------+--------+
| Variable0 | Value0 | Variable1 | Value1 | Variable2 | Value2 |
+-----------+--------+-----------+--------+-----------+--------+
| a         | 1      | c         | 3      | e         | 5      |
| b         | 2      | d         | 4      | f         | 6      |
+-----------+--------+-----------+--------+-----------+--------+