如何汇总列和行?

时间:2016-10-17 13:53:49

标签: sql oracle oracle11g aggregate-functions

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

1 个答案:

答案 0 :(得分:1)

"[{\"country_code\" : \"USA\", \"country_name\" : \"United States\",\"bac_limit\" : 0.80}, { \"country_code\" : \"CAN\", \"country_name\" : \"United States\",\"bac_limit\":0.80}]"