如何消除或合并聚合表中多行中的少行结果?
当前情况是第一个表#1有6行:
cntrc_key | re_key | aup_key | insr_key | drvr_key | 其他_key | lkp_key |
---|---|---|---|---|---|---|
58046828 | 0 | 0 | 0 | 0 | 0 | 0 |
58046828 | 0 | 0 | 0 | 0 | 127270 | 0 |
58046828 | 0 | 45803863 | 0 | 0 | 127270 | 0 |
58046828 | 0 | 45803957 | 0 | 58245216 | 0 | 0 |
58046828 | 0 | 0 | 51021791 | 0 | 0 | 0 |
58046828 | 0 | 45803863 | 0 | 58245215 | 0 | 0 |
预计下一个表#2 有 2 行:
cntrc_key | re_key | aup_key | insr_key | drvr_key | 其他_key | lkp_key |
---|---|---|---|---|---|---|
58046828 | 0 | 45803863 | 51021791 | 58245215 | 127270 | 0 |
58046828 | 0 | 45803957 | 51021791 | 58245216 | 0 | 0 |
您可以使用 listagg 获取每个 cntrc_key 的所有行作为一行。 将整行转换为几行将消除数据的含义。
我尝试了以下方法。
select
cntrc_key
,LISTAGG(distinct re_key, ',') WITHIN GROUP (ORDER BY re_key) as re_key
,LISTAGG(distinct AUP_KEY, ',') WITHIN GROUP (ORDER BY AUP_KEY) as AUP_KEY
,LISTAGG(distinct INSR_KEY, ',') WITHIN GROUP (ORDER BY INSR_KEY) as INSR_KEY
,LISTAGG(distinct DRVR_KEY, ',') WITHIN GROUP (ORDER BY DRVR_KEY) as DRVR_KEY
,LISTAGG(distinct OTHR_KEY, ',') WITHIN GROUP (ORDER BY OTHR_KEY) as OTHR_KEY
,LISTAGG(distinct LKP_KEY, ',') WITHIN GROUP (ORDER BY LKP_KEY) as LKP_KEY
from table_name
group by cntrc_key;
CNTRC_KEY | RE_KEY | AUP_KEY | INSR_KEY | DRVR_KEY | OTHR_KEY | LKP_KEY |
---|---|---|---|---|---|---|
58046828 | 0 | 0,45803863,45803957 | 0,51021791 | 0,58245215,58245216 | 0,127270 | 0 |
请告诉我这是否有帮助。或者让我知道您希望输出的规则。