2017-01-30 6 views
2

私はsql言語で初心者ですし、データを再配置するためにいくつかの助けが必要です。ピボットの代わりに

(私はSQL Server 2008ので働いています)私は、このテーブル(代替)があります。

 
iteid | substitutedescr | substitutecode 
37664 | EANCUTIE3  | 14902778788926 
37664 | EAN1    | 4902778788929 
37664 | EANCUTIE1  | 4902778931653 
37664 | EANCUTIE2  | 4902778931738 

をし、私はこのように見える選択したい:

 
iteid EAN1   EANCUTIE1   EANCUTIE2  EANCUTIE3 
37664              14902778788926 
37664 4902778788929   
37664     4902778931653  
37664          4902778931738 

Iピボットを使用しようとしました:

select * 
from (
      select iteid as [ID], substitutedescr as [descr], substitutecode  as [Values] 
      from substitute) as s 
PIVOT 
(
SUM(SUBSTITUTECODE) 
FOR [DESCR] in (ean1, ean2, ean3, eancutie1, eancutie2, eancutie3) 
) as pvt 

しかしピボット機能を有効にするためには互換性レベルを高い値に設定する必要があるようです。

私はこの結果を得るための他の選択肢がありますか?

ありがとうございます。

答えて

5

あなただけcase、このためpivotは必要ありません。

select iteid, 
     (case when substitutedescr = 'EAN1' then substitutecode end) as EAN1, 
     (case when substitutedescr = 'EANCUTIE1' then substitutecode end) as EANCUTIE1, 
     (case when substitutedescr = 'EANCUTIE2' then substitutecode end) as EANCUTIE2, 
     (case when substitutedescr = 'EANCUTIE3' then substitutecode end) as EANCUTIE3 
from substitute; 

あなたは、その行のすべての値でiteidごとに1つの行を望んでいた場合は、pivot(または凝集)を望みます。例:

select iteid, 
     max(case when substitutedescr = 'EAN1' then substitutecode end) as EAN1, 
     max(case when substitutedescr = 'EANCUTIE1' then substitutecode end) as EANCUTIE1, 
     max(case when substitutedescr = 'EANCUTIE2' then substitutecode end) as EANCUTIE2, 
     max(case when substitutedescr = 'EANCUTIE3' then substitutecode end) as EANCUTIE3 
from substitute 
group by iteid; 
+0

ありがとうございました:D私の問題を解決しました。 –