現在、縦持ちのデータで
qualifications_verticalテーブル
employee_id qualification_id
100 1000
100 2000
100 3000
100 4000
101 1000
102 2000
102 3000
102 4000
103 3000
103 4000
104 1000
104 2000
104 4000
になっているとします。
これを以下のように横持ちにしたいとします。
employee_id qualification_id1 qualification_id2 qualification_id3 qualification_id4
100 1000 2000 3000 4000
101 1000 NULL NULL NULL
102 2000 3000 4000 NULL
103 3000 4000 NULL NULL
104 1000 2000 4000 NULL
この時のSQL文は
select
tmp.employee_id
,max(case tmp.seq when 1 then tmp.qualification_id else null end) as qualification_id1
,max(case tmp.seq when 2 then tmp.qualification_id else null end) as qualification_id2
,max(case tmp.seq when 3 then tmp.qualification_id else null end) as qualification_id3
,max(case tmp.seq when 4 then tmp.qualification_id else null end) as qualification_id4
from
(
select
employee_id
,qualification_id
,row_number() over (partition by employee_id) as seq
from
qualifications_vertical
) tmp
group by
tmp.employee_id
;
です。
http://dev.classmethod.jp/etc/sql-data-horizontal-vertical/
をご参照
SQL文に関しては
http://www.sql-reference.com/
などをご参照
ご参考まで
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。