2016-02-25 23 views
4

(總有組合的2個重複):T-SQL 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進行動態查詢,但無法得出令人滿意的結果。

有人能請指教嗎? (0,0,0),(1,1,1),(()),((),())的結果是否可能達到相同的結果,但是Ullas解決方案完美地適用於組合對, 2,2,2)應該導致3行)?我認爲動態查詢仍然是一種可行的方式,可能這次是與PIVOT合作。

+0

將你的輸出始終包含固定數量的Ø f列? –

+0

不,柱子的數量應該是組合數量的兩倍。 – xmamat

+0

然後你將不得不去動態SQL路由。非動態SQL將始終返回固定數量的列。這意味着PIVOT不在桌面上。下面是@Ullas的一個很好的例子。 –

回答

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  | 
+-----------+--------+-----------+--------+-----------+--------+ 
+0

這很出色!謝謝你,我的好先生。 – xmamat