テーブルには、すべての個別の値を持つ列が 1 つしかありません。それを 3 つのペアにグループ化し、3 つの行から 3 つの列を作成する必要があります。助けてください
ソース
COL1
-----
A
B
C
D
E
F
必要な出力 1:
COL1
------
A,B,C
D,E,F
必要な出力 2:
col1 col2 col3
---- ---- ----
A B C
D E F
出力 1:
select listagg(col1, ',') within group (order by col1) as col
from (
select col1,
case
when row_number() over (order by col1) <= (count(*) over ()) / 2 then 0
else 1
end as grp
from foo
)
group by grp
order by grp;
出力 2 の場合:
select max(col1) as col1,
max(col2) as col2,
max(col3) as col3
from (
select case mod(row_number() over (order by col1),3)
when 1 then col1
else null
end as col1,
case mod(row_number() over (order by col1),3)
when 2 then col1
else null
end as col2,
case mod(row_number() over (order by col1),3)
when 0 then col1
else null
end as col3,
case
when row_number() over (order by col1) <= (count(*) over ()) / 2 then 0
else 1
end as grp
from foo
)
group by grp
order by grp;
SQLFiddle の例: http://sqlfiddle.com/#!4/d699c/1
別の方法 (レコードの 3 の倍数ごとに機能します)
出力1
select listagg(col1, ',') within group (order by col1) col1
from
(select col1, row_number() over(order by col1) rn
from t) tt
group by rn - decode(mod (rn, 3) ,0,3,mod (rn, 3));
出力2
select c2_col col1, c3_col col2, c1_col col3
from
(select rn - decode(mod (rn, 3) ,0,3,mod (rn, 3)) grp, mod(rn, 3) rnm, col1
from
(select col1, row_number() over(order by col1) rn
from t)) tt
pivot
(
max(col1) as col
for rnm in (0 as c1,1 c2,2 c3)
);