2017-09-20 3 views
0

Ich habe die folgende Abfrage, die zwei Ergebnisse anzeigt.Oracle Query - Zusammenführen mehrerer Ergebnisse in einer einzigen Zeile

Abfrage:

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

Ergebnis:

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

Ich mag würde diese Ergebnisse in dem folgende Format verschmolzen holen.

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

Kann ich dies durch eine Oracle-Abfrage erreichen?

+0

Wird es nur zwei Rollen pro contract_id sein? – GurV

+0

Ja @ GurV! Genau – Nidheesh

Antwort

1

können Sie verwenden row_number und Aggregation die erforderliche mehrspaltigen Verschwenkung zu bekommen:

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 
+0

Vielen Dank @GurV! – Nidheesh

Verwandte Themen