2016-11-29 15 views
0

自学,首次海报。
我有什么似乎应该是基本的,但我有一个让大脑工作的障碍。我有两个数据集,我想将其中一个与另一个进行比较,并找到集合1(实际)中未设置为(操作费用)的记录​​。子查询上的SQL多重特征选择

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

这将返回一个数据集,看起来像这样:

Operating Expense Accounts

我的第二个查询:

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。

我只是不确定如何匹配多个特征。

这个查询在我只查看一个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请阅读[**如何提问**](http://stackoverflow.com/help/how-to-ask) \t \t这里是[** START **]( 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') 

我不得不重新排列并调整您的查询以使字段匹配,并且我必须从select中删除一个。但是,这应该会为您提供所需的信息,并且从那里您应该可以获得相关记录所需的任何其他数据。

如果出于某种原因,这是不是一种选择,你也可以尝试这样的:

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 

这是粗略的代码,因为我不知道你的表中的实际模样,但我相信也应该给你你需要的东西。我倾向于尽可能避免次选,这就是为什么我更喜欢第一种选择。

我很抱歉,如果我将记录集倒退,我不太确定是否按照您设置的方式进行了操作。如果我这样做,只需翻阅查询以获得所需的结果。