2017-11-17 16 views
1

私は以下の構造を持っています。 | id | value |列に私のSQL行をピボット

| a | v1 |

| b | v2 |

| c | v3 |

| d | v4 |

出力が必要です。

| a | b | c | d |

| v1 | v2 | v3 | v4 |

私がアドバイスしてください多くを試みたが、成功

select case when t1.id = 'a' then t1.value end as a , 
     case when t1.id = 'b' then t1.value end as b , 
      case when t1.id = 'c' then t1.value end as b , 
      case when t1.id = 'd' then t1.value end as d 


      from 
t1 

を得ることができます。あなたは必要な結果の集計関数を使用する

おかげ

答えて

1

は、単一のグループ

として、テーブル全体を検討することにより、グループなしで集約関数を使用して

select max(case when t1.id = 'a' then t1.value end) as a , 
     max(case when t1.id = 'b' then t1.value end) as b , 
     max(case when t1.id = 'c' then t1.value end) as b , 
     max(case when t1.id = 'd' then t1.value end) as d 
from t1 

Demo

に注意を設定します

+0

ありがとうございました –

関連する問題