2016-11-29 12 views
0

自己紹介、初めてのポスターです。
私はそれが基本的なはずのように思えますが、これを動作させるには脳のブロックがあります。私は2つのデータセットを持っていますが、私はもう一方を比較して、セット1(実績)にあるレコードを(営業経費)に設定されていないものにしたいと思います。SQLで複数の特性を選択してください。

select distinct vacctcode, vEntityID 
from  tm1_fin_lines_integration 
where vScenario = '507' 
    and vLineItemType = 'OperatingExpense' 

これは、このようになります。データセットを返します。

Operating Expense Accounts

私の2番目のクエリは、次のとおりです。

select i.property_externalid, i.externalid, r.lineitemtypename 
from ivw_Actuals i 
inner join rvw_accounts r on r.accountnumber = i.externalid 
where r.LineItemTypeName = 'OperatingExpense' 
and i.PropertyAssetId in . 
(select vpropid from tm1_fin_lines_integration where vScenario = '507') 

とリターンの結果のように:

Actuals

運用経費の結果セットにない実績結果セットのレコードを検索したいとします。私はexternalid/vacctcodeとvEntityid/Property_ExternalIDで一致する必要があります。

どのように複数の特性にマッチするか分かりません。

このクエリは、1つのvEntityid/Property_ExternalIDを見ているときに機能しますが、実際には何百ものデータセットが表示されるため、アカウントコードとエンティティの組み合わせを見つける必要があります。営業費用セットに含まれていない実績セット。

select i.property_externalid, 
i.externalid, 
r.lineitemtypename 
from ivw_Actuals i 
inner join rvw_accounts r on r.accountnumber = i.externalid 
where r.LineItemTypeName = 'OperatingExpense' 
and i.PropertyAssetId in 
(select vpropid from tm1_fin_lines_integration where vScenario = '507') 
and i.externalid not in 
(select distinct vacctcode, vEntityID 
from 
tm1_fin_lines_integration 
where 
vScenario = '507' 
and vLineItemType = 'OperatingExpense') 
+0

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

+0

サンプルデータと期待される結果を見せてください。実際の結果セットに何が「運営経費にはない」とはっきりしない。 \t [** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t [**スタート**] http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)質問品質を向上させ、より良い回答を得る方法を学ぶことができます。 –

答えて

0

NOT EXISTS制約試してみてください。

select distinct vacctcode, vEntityID 
from 
tm1_fin_lines_integration x 
where vScenario = '507' 
and vLineItemType = 'OperatingExpense' 

AND NOT EXISTS (

     select i.property_externalid, i.externalid, r.lineitemtypename 
     from ivw_Actuals i 
     inner join rvw_accounts r on r.accountnumber = i.externalid 
     where r.LineItemTypeName = 'OperatingExpense' 
     and i.PropertyAssetId in (select vpropid from tm1_fin_lines_integration where vScenario = '507') 

     AND i.externalid = x.vacctcode 
     AND i.property_externalid = x.vEntityID 

) 
0

あなたはexceptキーワードを使用することができます。このように:

select distinct vacctcode, vEntityID 
from  tm1_fin_lines_integration 
where vScenario = '507' 
    and vLineItemType = 'OperatingExpense' 

except 

select i.externalid, i.property_externalid 
from ivw_Actuals i 
inner join rvw_accounts r on r.accountnumber = i.externalid 
where r.LineItemTypeName = 'OperatingExpense' 
and i.PropertyAssetId in . 
(select vpropid from tm1_fin_lines_integration where vScenario = '507') 

私が一致するフィールドを取得するためにクエリを再配置し、調整しなければならなかった、と私は選択から1を削除する必要がありました。しかし、これはあなたが必要なものを得るはずです。そこから、関連レコードに必要な他のデータを得ることができます。

どんな理由であれ、それはオプションではありません、場合、あなたはまた、このような何かを試みることができる:私はあなたのテーブルがどのように見える内容を正確に把握していないとしてそれは、大まかなコードです

select distinct vacctcode, vEntityID, act.* 
    from  tm1_fin_lines_integration fin 
    left join (
    select i.externalid, i.property_externalid 
    from ivw_Actuals i 
    inner join rvw_accounts r on r.accountnumber = i.externalid 
    where r.LineItemTypeName = 'OperatingExpense' 
    and i.PropertyAssetId in . 
    (select vpropid from tm1_fin_lines_integration where vScenario = '507') 
) act on act.externalid = fin.vacctcode and act.property_externalid = fin.vEntityID 
    where vScenario = '507' 
     and vLineItemType = 'OperatingExpense' 
     and act.externalid is null 

を、しかし、私はそれがまたあなたに必要なものを与えるべきだと信じています。可能であれば、私はサブ選択を避けようとする傾向があります。それが私が最初の選択肢を好む理由です。

私はレコードセットを後ろに持っていればお詫び申し上げますが、私はあなたが設定した通りの方法を踏襲しているとは思っていませんでした。そうした場合、必要な結果を達成するためにクエリを前後に移動するだけです。

関連する問題