2013-03-27 58 views
0

我試過但無法返回所有行。它只返回左側查詢中的一些行。請找到錯誤。與左連接卡住

SELECT c.star_ident, c.fix_ident 
from corept.std_star_leg as c 
INNER JOIN 
(
    SELECT star_ident, 
    transition_ident, 
    max(sequence_num) seq, 
    route_type 
    FROM  corept.std_star_leg 
    WHERE data_supplier='J' 
    AND airport_ident='KOPF' 
    group by star_ident,transition_ident 
) b 
    ON c.sequence_num=b.seq 
    and c.star_ident=b.star_ident 
    and c.transition_ident=b.transition_ident 
LEFT OUTER JOIN 
(
    SELECT name, 
    trans 
    FROM skyplan_deploy.deploy_stars d 
    WHERE apt='KOPF' 
    AND name!=trans 
) as x 
    on x.name=c.star_ident 
    and x.trans=c.transition_ident 
where c.data_supplier='J' 
    and c.airport_ident='KOPF' 
    and x.name is null; 

corept.std_star_leg表是這個。

star_ident transition_ident sequence_num fix_ident airport 
A    XX    10   QWE  KOPF 
A    XX    20   WER  KOPF 
A    XX    30   HYU  KOPF 
A    XX    40   GJI  KOPF 
B    YY    10   SJI  KOPF 
B    YY    20   DJI  KOPF 
B    YY    30   FJI  KOPF 
B    YY    40   GHI  KOPF 
B    YY    50   KDI  KOPF 

執行內部連接後,將獲得如下結果。

A    XX    40   GJI 
B    YY    50   KDI 

因此檢索最大sequence_num行。之後,skyplan_deploy.deploy_stars表格將如下所示。

apt   name    trans  
KOPF   A     FJI 
KOPF   A     DHI 
KOPF   B     VNM 

我需要輸出

A GJI 
B KDI 
+0

你認爲你可以添加一些東西來重新創建表或將它放在sqlfiddle?有點難以測試查詢這種方式 – 2013-03-27 03:28:42

+0

如果您需要輸出GJI和KDI,並且內部關節獲得兩者,爲什麼不輸出那個? – Patashu 2013-03-27 03:29:33

+0

ü沒有得到我..它只是一個例子...我無法o/p既GJI和KDI .. @ Patashu – user2037445 2013-03-27 03:31:49

回答

2

這可能work.Check一次。

SELECT DISTINCT c.airport_ident,c.sid_ident,c.transition_ident,c.fix_ident 
       FROM corept.std_sid_leg c 
       INNER JOIN 
       (SELECT sid_ident,transition_ident,max(sequence_num) seq,route_type 
       FROM corept.std_sid_leg 
       WHERE data_supplier='J' 
       AND airport_ident='KOPF' 
       GROUP BY sid_ident,transition_ident)b 
       ON c.sequence_num=b.seq AND c.sid_ident=b.sid_ident AND c.transition_ident=b.transition_ident 
       LEFT JOIN 
       (SELECT name,trans 
        FROM skyplan_deploy.deploy_sids 
        WHERE apt='KOPF' 
        AND name!=trans) d 
       ON d.name=c.sid_ident AND d.trans=c.fix_ident 
       WHERE c.data_supplier='J' AND c.airport_ident='KOPF' AND d.name is null