2016-12-02 1 views
0

Le circuit a deux extrémités (jambes) et chaque jambe est connectée à l'appareil. SO résultat est symétrique à un circuit de chaque côté dire et B. Je cherche à trier mes résultats SQL comme ci-dessous:oracle ordre par: résultats de la commande symétriquement

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 

. ma requête est:

sortie réelle
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; 

était:

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

Vous sélectionnez 4 colonnes, mais seulement nous montrent trois d'entre eux. Celui que vous avez commandé est manquant ... – jarlh

+0

Quel est le résultat attendu si vous ajoutez des éléments C? – jarlh

+0

Vous ne manquez pas un leg_A dans votre exemple? –

Répondre

0

Il suffit de sélectionner les données des tables et coller les résultats ensemble. Comme vous connaissez tous les emplacements possibles à l'avance, vous pouvez utiliser une expression de cas pour appliquer un ordre de tri.

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; 

Il serait préférable d'avoir une table de recherche pour les cinq emplacements et leurs clés de tri à la place.

+0

Merci @Thorsten kettner !!! Cela m'a aidé. – user3427970

0

Enfin, la condition de cas imbriquée a fonctionné dans ce cas. Voici la requête modifiée.

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; 

Résultat:

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