2017-06-13 15 views
0

に行を回す:Oracleは、私はこのようになりますテーブルを持つ列

station  node  group 
13859160 13195576 high 
13859160 42502030 low 
13859165 42283197 low 
13859165 42283198 high 
13859166 13190800 low 
13859166 13190801 low 
13859166 13195587 high 

それは、このステートメントを使用して作成することができます。

CREATE TABLE nodes 
    ("station" int, "node" int, "group" varchar(6)) 
; 

と、この文でいっぱい:

insert into nodes values (13859160, 13195576, 'high'); 
insert into nodes values (13859160, 42502030, 'low'); 
insert into nodes values (13859165, 42283197, 'low'); 
insert into nodes values (13859165, 42283198, 'high'); 
insert into nodes values (13859166, 13190800, 'low'); 
insert into nodes values (13859166, 13190801, 'low'); 
insert into nodes values (13859166, 13195587, 'high'); 

このテーブルを照会して、上位と下位が列になるようにして、結果が次のようになるようにします。

Station  high  low 
13859160 13195576 42502030 
13859165 42283198 42283197 
13859166 13195587 13190801 
13859166 13195587 13190800 

複数の「低」の行を持っていた何のステーションが、それは容易ではないだろうところ、私はハイとローを取得するには、このような何かを行うことができますが場合:

になり
select 
    "station", 
    case when "group" = 'high' then "node" end as high, 
    case when "group" = 'low' then "node" end as low 
from "NODES"; 

enter image description here

、その後に結果を取得するには、集計関数を使用するもののように:

select 
    "station", 
    max(case when "group" = 'high' then "node" end) as high, 
    max(case when "group" = 'low' then "node" end) as low 
from "NODES" 
group by "station"; 

これはもたらす:ただし、1 startionは、複数の「低」entiesを有する場合、Iがそれぞれ低エントリごとに1行を必要と、その行はそのため、同じ「高」のエントリを含める必要があり

enter image description here

両方の行のステーション。

どのようにすればよいでしょうか?

+0

これはよくある質問です。いくつかの[oracle + pivot'の既存のスレッド](https://stackoverflow.com/search?q=%5Boracle%5D+pivot) – APC

+0

を参照してください。複数の高低がある場合はどうなりますか? –

+0

そこには1つのステーションあたり1つの高さしかありませんが、複数の低 – ErikL

答えて

0

まず、私は集計クエリのlistagg()を示唆するかもしれません:

select "station", 
     listagg(case when "group" = 'high' then "node" end) as high, 
     listagg(case when "group" = 'low' then "node" end) as low 
from "NODES" 
group by "station"; 

シングルステーションのための複数の行を取得するには、row_number()を使用することができます。

最後に
select "station", 
     max(case when "group" = 'high' then "node" end) as high, 
     max(case when "group" = 'low' then "node" end) as low 
from (select n.*, 
      row_number() over (partition by "station", "group" order by "node") as seqnum 
     from "NODES" n 
    ) n 
group by "station", seqnum; 

、あなたの場合最後の最大値を繰り返す:

select "station", 
     coalesce(max(case when "group" = 'high' then "node" end), 
       max(max(case when "group" = 'high' then "node" end)) over (partition by "station) 
       ) as high, 
     max(case when "group" = 'low' then "node" end) as low 
from (select n.*, 
      row_number() over (partition by "station", "group" order by "node") as seqnum 
     from "NODES" 
    ) n 
group by "station", seqnum; 
0

条件付き集計でこれを行うことができます。

select * from (
select 
    "station", 
    max(case when "group" = 'high' then "node" end) over(partition by "station") as high, 
    case when "group" = 'low' then "node" end as low 
from nodes 
) n 
where low is not null 
関連する問題