繁体   English   中英

Oracle查询-在一行中合并多个结果

[英]Oracle query - merging multiple results in a single row

我确实有以下查询,其中显示了两个结果。

查询:

select /*+ parallel(16) */ * from CONTRACT where CONTRACT_ID ='1234';

结果:

_____________________________________________________________________________________
|CONTRACT_SOURCE    |   CONTRACT_ID |   ROLE        |   ROLE_ID |   STD_CD  |   INDEX
_____________________________________________________________________________________
|Source             |   1234        |   role_driver |   unique1 |   LOAD    |   9
|Source             |   1234        |   role_insured|   unique2 |   LOAD    |   9
_____________________________________________________________________________________

我想将这些结果合并到以下格式中。

_____________________________________________________________________________________________________________________
|CONTRACT_SOURCE    |   CONTRACT_ID |   ROLE        |   ROLE_ID |   ROLE            |   ROLE_ID | STD_CD | INDEX    |
_____________________________________________________________________________________________________________________
|Source             |   1234        |   role_driver |   unique1 |   role_insured    |   unique2 | LOAD   | 9        |
_____________________________________________________________________________________________________________________

我可以通过Oracle查询来实现吗?

您可以使用row_number和aggregation获得所需的多列数据透视:

select contract_source, 
    contract_id, 
    std_cd, 
    in,
    max(case when rn = 1 then role end) as role_1,
    max(case when rn = 1 then role_id end) as role_id_1,
    max(case when rn = 2 then role end) as role_2,
    max(case when rn = 2 then role_id end) as role_id_2
from (
    select c.*,
        row_number() over (
            partition by contract_source, contract_id, std_cd, in
            order by role_id
            ) as rn
    from contract c
    ) t
group by contract_source, contract_id, std_cd, in

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM