listagg
работает, как ожидается, для poll_id=2
.Как заполнить столбцы, а также строки?
Как я могу использовать то же самое для одной строки и нескольких столбцов?
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 имеет только одну строку в наборе результатов. Я хочу игнорировать нули.
1 Apple;Pear;Peach;Plum
2 Cherry;Grape;Kiwi
3 Squash;Peas;Barley;Oats
В вашем примере, для 'poll_id = 2', все три фрукты "'first_choice'". В каком порядке они должны появиться на выходе? (Имеет ли это значение? Например, они могут появляться в алфавитном порядке.) В противном случае, я предполагаю, что для любого заданного 'poll_id' значения, указанные в' first_choice', должны отображаться первыми, а затем в 'second_choice' и т. Д.? – mathguy
Да, порядок имеет значение. Для каждого id, идите 1, 2, 3, 4-й выбор. Затем вторая строка, затем третья строка. и т. д. – zundarz
@zundarz Я обновил свой ответ ниже. –