Oracle sql listagg基于另一列

时间:2016-03-20 04:48:54

标签: sql oracle oracle11g listagg

我是Oracle SQL的新手,我需要处理如下任务。

我在这里附上了一张图片,我现在需要做的是,使用SQL从左侧显示右侧的结果..

我需要根据第1栏(C1)列出第2列(C2)和第3列(C3)的列表 如果C2和C3只有一条记录而没有重复记录,则 结果显示如第一行:4316,Orange shipper 1,中国香港 然而,在C1中有一些棘手的案例如4343/4403/4406 C2可能有重复的记录,但C3没有,反之亦然。

我尝试使用rank_over,然后结果成为附加图像中的左侧。

SELECT t1.number AS C1,
       t2.shipper AS C2,
       rank() over (partition BY t2.shipper
                    ORDER BY t1.number) AS rank_over_c2,
       t2.venue AS C3,
       rank() over (partition BY t2.venue
                    ORDER BY t1.number) AS rank_over_c3
FROM table1 t1,
     table2 t2

但在那之后我不知道如何在附图中得到像右手那样的结果..

Image在这里:
左手边是原始列和数据,我想得到像右手边的结果

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT * FROM 
(
   SELECT c1, listagg( c2, ', ' ) within group ( order by c2 ) as C2
   FROM ( select distinct c1, c2 from c1c2 )
   Group by c1
) x 
JOIN (
   SELECT c1, listagg( c3, ', ' ) within group ( order by c3 ) as C3
   FROM ( select distinct c1, c3 from c1c2 )
   Group by c1
) y
using( c1 )