2017-02-02 35 views
0

からレコードを除外するためにどのようなシナリオです: 私はtblSalesOrderとから 'sales_id' で参加tblSalesOrder、tblAcccountExecSummaryという名前の3つのテーブルとSQLクエリ - 別のテーブルここで

tblAccountExecStatus tblAccountExecStatus

を持っています " idはtblAccountExecSummaryから、私のプログラムに表示するために使用されます。

tblAccountExecSummaryには、複数のアカウントエグゼクティブが含まれている場合があります。

上記の例では、結合されたテーブルには、1つの受注のアカウント幹部が2名あります。

tblAccountExecStatus tblAccountExecStatus

私は唯一の「2」のアカウントエグゼクティブのステータスを持って参加しました表を表示します。意味受注記録に少なくとも1人の口座管理者のステータスが「1」である場合、レコードを除外します。

私はこれに関する正しい質問を探していますが、まだ私はまだそれを持っていません。私は本当にあなたの助けに感謝します。

それはtblSalesOrder

+1

where句には「not exists」を使用します。 –

+0

どのDBMSを使用していますか? –

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-wing-asking-a-question/285557#285557 –

答えて

0

のすべてのデータが次のことを試してみてください表示になります:内側のクエリでは、状況1account_executiveですべてのサマリーエントリを準備します。この内部クエリsummwith1と呼ぶことにしましょう。次に、外部クエリで、idのIDにsumwith1が含まれていないサマリーエントリを返します。

select * from tblAccountExecSummary summary 
where not exists 
    (select 1 from tblAccountExecStatus status join tblAccountExecSummary sumwith1 on status.account_executive = sumwith1.account_executive 
    where status.status = 1 
    and sumwith1.id = summary.id 
) 
関連する問題