2016-12-02 3 views
0

回路は2つの終点(脚)を持ち、各脚はデバイスに接続されています。オラクルオーダーby:対称的な結果の注文

component location pathelement 
------------------------------- 
device_A A  A_DD1 
device_A A  A_DD2 
device_A A  A_DD3 
leg_A  L_A  A_LL1 
leg_A  L_A  A_LL2 
circuit  Center CCCC 
leg_B  L_B  B_LL1 
leg_B  L_B  B_LL2 
device_B B  B_DD2 
device_B B  B_DD3 

:その結果、各側の回路に対称はAとB が、私は以下のように私のSQLの結果をソートするために探していますと言っています。 私のクエリは次のとおりです。

select component,location,pathelement,sortorder from 
(select c.name circuit,l.name leg,d.name d, 
c.location c_loc,l_pe.location l_loc,d_pe.location d_loc, 
c_pe.pathelements c_pe,l_pe.pathelements l_pe,d_pe.pathelements d_pe, 
1 d_sortorder,2 l_sortorder,3 c_sortorder 
from circuit c,leg l,device d, 
(select circuitid,location,pathelements from circuit_details) c_pe, 
(select legid,location,pathelements from leg_details)l_pe, 
(select deviceid,location,pathelements from device_details)d_pe 
where 
c.circuit2leg=l.legid(+) 
and l.leg2device=d.deviceid(+) 
and c.circuitid=c_pe.circuitid(+) 
and l.legid=l_pe.legid(+) 
and d.deviceid=d_pe.deviceid(+) 
and c.name=<some_text>) 
Unpivot((component,location,pathelement,sortorder) for c in 
((circuit,c_loc,c_pe,c_sortorder),(leg.l_loc,l_pe,l_sortorder),(device,d_loc,d_pe,d_sortorder))) order by sortorder; 

実際の出力は:

component location pathelement 
------------------------------- 
device_A A  A_DD1 
device_A A  A_DD2 
device_A A  A_DD3 
device_B B  B_DD2 
device_B B  B_DD3 
leg_A  L_A  A_LL1 
leg_A  L_A  A_LL2 
leg_B  L_B  B_LL1 
leg_B  L_B  B_LL2 
circuit  Center CCCC 
+1

あなたは4列を選択し、だけ私たちにそれらの3を示しました。あなたが注文したものが欠落しています... – jarlh

+0

Cの項目を追加したらどうなるでしょうか? – jarlh

+0

あなたの例ではleg_Aを見逃しませんか? –

答えて

0

は、単にテーブルからデータを選択して、結果を一緒に接着します。可能なすべての場所を事前に知っているので、ケース式を使用してソート順を適用できます。

select c.name, cd.location, cd.pathelement 
from circuit c 
join circuit_details cd on cd.circuitid = c.circuitid 
where c.name = <some_text> 
UNION ALL 
select l.name, ld.location, ld.pathelement 
from leg l 
join leg_details ld on ld.circuitid = l.circuitid 
where l.circuitid = (select c.circuitid from circuit c where c.name = <some_text>) 
UNION ALL 
select d.name, dd.location, dd.pathelement 
from device d 
join device_details dd on dd.circuitid = d.circuitid 
where d.circuitid = (select c.circuitid from circuit c where c.name = <some_text>) 
ORDER BY 
    case location 
    when 'A' then -2 
    when 'L_A' then -1 
    when 'Center' then 0 
    when 'L_B' then 1 
    when 'B' then 2 
    end, 
    pathelement; 

代わりに、5つの場所とそのソートキーのルックアップテーブルを用意することをお勧めします。

+0

ありがとう@Thorsten kettner !!!それは私を助けた。 – user3427970

0

最後に、ネストされたケース条件がこの場合に機能しました。変更されたクエリは次のとおりです。

select component,location,pathelement, 
case when location='A' then 
case when LTRIM(component,'_A')='device' then -2 
when LTRIM(component,'_A')='leg' then -1 end 
when location='Center' then 0 
when location='B' then 
case when LTRIM(component,'_B')='device' then 2 
when LTRIM(component,'_B')='leg' then 1 end as ord 
from 
(select c.name circuit,l.name leg,d.name d, 
c.location c_loc,l_pe.location l_loc,d_pe.location d_loc, 
c_pe.pathelements c_pe,l_pe.pathelements l_pe,d_pe.pathelements d_pe 
from circuit c,leg l,device d, 
(select circuitid,location,pathelements from circuit_details) c_pe, 
(select legid,location,pathelements from leg_details)l_pe, 
(select deviceid,location,pathelements from device_details)d_pe 
where 
c.circuit2leg=l.legid(+) 
and l.leg2device=d.deviceid(+) 
and c.circuitid=c_pe.circuitid(+) 
and l.legid=l_pe.legid(+) 
and d.deviceid=d_pe.deviceid(+) 
and c.name=<some_text>) 
Unpivot((component,location,pathelement,sortorder) for c in 
((circuit,c_loc,c_pe),(leg.l_loc,l_pe),(device,d_loc,d_pe))) order by ord,pathelement; 

結果:

component location pathelement ord 
------------------------------- 
device_A A  A_DD1  -2 
device_A A  A_DD2  -2 
device_A A  A_DD3  -2 
leg_A  L_A  A_LL1  -1 
leg_A  L_A  A_LL2  -1 
circuit  Center CCCC  0 
leg_B  L_B  B_LL1  1 
leg_B  L_B  B_LL2  1 
device_B B  B_DD2  2 
device_B B  B_DD3  2 
関連する問題