listagg
は、poll_id=2
の期待どおりに機能します。列と行を集計する方法は?
1行と複数列で同じように使用できますか?
with list_of_fruits as
(select 1 as poll_id ,'Apple' as first_choice, 'Pear' as second_choice, 'Peach' as third_choice, 'Plum' as fourth_choice from dual union all
select 2 as poll_id ,'Cherry' as first_choice, null as second_choice, null as third_choice, null as fourth_choice from dual union all
select 2 as poll_id ,'Grape' as first_choice, null as second_choice, null as third_choice, null as fourth_choice from dual union all
select 2 as poll_id ,'Kiwi' as first_choice, null as second_choice, null as third_choice, null as fourth_choice from dual union all
select 3 as poll_id ,'Squash' as first_choice, 'Peas' as second_choice, '' as third_choice, null as fourth_choice from dual union all
select 3 as poll_id ,null as first_choice, null as second_choice, null as third_choice, 'Barley' as fourth_choice from dual union all
select 3 as poll_id ,null as first_choice, null as second_choice, 'Oats' as third_choice, null as fourth_choice from dual
) select poll_id, listagg(first_choice,';') within group (order by poll_id) as sam
from list_of_fruits
group by poll_id
所望の出力
Consolodate各poll_idが結果セット内の唯一の行を有するように(右、上から下へ左)は、すべての行と列。私はnullを無視したい。あなたの例では
1 Apple;Pear;Peach;Plum
2 Cherry;Grape;Kiwi
3 Squash;Peas;Barley;Oats
、 'poll_id = 2 'のために、すべての3つの果実は "'first_choice'" です。出力にどのような順序で表示する必要がありますか?そうでなければ、与えられた 'poll_id'に対して、' first_choice'で与えられた果実が最初に現れ、次に 'second_choice'などで果実が現れると仮定します。 – mathguy
はい注文は問題ありません。それぞれのIDに対して、1位、2位、3位、4位に進みます。その後、2行目、3行目。 – zundarz
@ zundarz私は以下のように私の答えを更新しました。 –