Sub查询上的SQL多特征选择

时间:2016-11-29 14:27:51

标签: sql

自学,第一次海报。
我觉得它应该是基本的,但我正在努力让它发挥作用。我有两个数据集,我想比较一个,并找到第1组(实际值)中未设置为(运营费用)的记录​​。

    create or replace function get_route2(snode integer, tnode integer)
returns setof geometry as
$$
declare 
    snode1 integer;
    tnode2 integer;
begin
    snode1 := snode;
    tnode2 := tnode;

    return query
        select di.seq, di.node, di.edge, di.cost, a.geom
    from pgr_dijkstra(
    'select id, target, source, cost from pedroad', 
    snode1, tnode2,false) as di,pedroad a
    where di.node = a.source;
end
$$
language plpgsql;

这将返回如下所示的数据集:

Operating Expense Accounts

我的第二个问题是:

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

并返回如下结果:

Actuals

我想查找Actuals结果集中不在Operating Expense Result Set中的记录。我需要匹配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')

2 个答案:

答案 0 :(得分: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

)

答案 1 :(得分: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 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

这是粗略的代码,因为我不确切知道你的桌子是什么样的,但我相信这也应该能满足你的需求。我倾向于尽量避免使用子选择,这就是为什么我更喜欢第一种选择。

如果我的记录设置倒退,我道歉,我不太确定我是按照你设置的方式进行的。如果我这样做,只需翻转查询即可获得所需的结果。