3

2 つのクエリがあり、それらの結果セットの違いを見つける必要があります。私のクエリは次のとおりです。

select star_ident,transition_ident,fix_ident,min(sequence_num)
  from corept.std_star_leg c
  where airport_ident='KLAS' and data_supplier='J'
  group by star_ident,transition_ident;

select name,trans
  from skyplan_deploy.deploy_stars
  where apt='KLAS';

これらは私の 2 つのクエリです。最初は左結合を使用しましたが、結果を取得できませんでした。

select star_ident,transition_ident,fix_ident,min(sequence_num)
  from corept.std_star_leg c
  left join
  (
    select name,trans
    from skyplan_deploy.deploy_stars
    where apt='KLAS' and name != trans
  ) a
 on star_ident=a.name and fix_ident=a.trans
 where airport_ident='KLAS' and data_supplier='J' and a.name is null
 group by star_ident,transition_ident;

上記のクエリを試しましたが、完全に間違った結果セットが返されます。これを作成するのを手伝ってくれますか?

ありがとうございました。

4

1 に答える 1

2

実際のテーブルなしでテストするのは難しいですが、それを行う 1 つの方法は次のようなものです。

SELECT t1.*
FROM (
  select star_ident,transition_ident,fix_ident,min(sequence_num)
  from std_star_leg c
  where airport_ident='KLAS' and data_supplier='J'
  group by star_ident,transition_ident) t1
LEFT JOIN (select name,trans
  from deploy_stars
  where apt='KLAS') t2
ON t1.star_ident = t2.name AND t1.fix_ident = t2.trans
WHERE t2.trans IS NULL;

つまり、選択を名前付きサブ選択にラップし、LEFT JOINそれらの間で標準を実行します。これにより、2 番目の結果セットではなく、最初の結果セットにある行が表示されます。

編集:最初の副選択ではなく 2 番目の副選択にある行を検索するには、代わりにそれを a RIGHT JOIN、選択t2.*、および null チェックに変更できt1.fix_identます。

SELECT t2.*
FROM (
  select star_ident,transition_ident,fix_ident,min(sequence_num)
  from std_star_leg c
  where airport_ident='KLAS' and data_supplier='J'
  group by star_ident,transition_ident) t1
RIGHT JOIN (select name,trans
  from deploy_stars
  where apt='KLAS') t2
ON t1.star_ident = t2.name AND t1.fix_ident = t2.trans
WHERE t1.fix_ident IS NULL;
于 2013-05-22T11:36:51.213 に答える