EL应急数据的数据库查询

时间:2014-12-18 03:27:45

标签: oracle

我想在EL Contingency表上运行几个查询。

查找最早的数据年份和拥有数据的人(如在哪些实体中) 查找最新的数据年份和拥有数据的人(如在哪些实体中) 是否有多年的EL应变在PL_ESCALATION

中没有匹配的年份

当前脚本:

select a.accounting_entity_id,
       b.entity_short_name,
       min(a.fiscal_year) as lowest
  from dc.pl_el_contingency a
  left outer join emt.ae_entity b
    on (a.accounting_entity_id = b.accounting_entity_id)
 group by a.accounting_entity_id, b.entity_short_name;

select a.accounting_entity_id,
       b.entity_short_name,
       max(a.fiscal_year) as highest
  from dc.pl_el_contingency a
  left outer join emt.ae_entity b
    on (a.accounting_entity_id = b.accounting_entity_id)
 group by a.accounting_entity_id, b.entity_short_name;

SELECT e.accounting_entity_id
  FROM dc.pl_el_contingency e
  LEFT OUTER JOIN dc.PL_ESCALATION s2
    ON (e.fiscal_year != s2.fiscal_year)
 WHERE s2.fiscal_year IS not NULL
 group by e.accounting_entity_id;

1 个答案:

答案 0 :(得分:0)

您的第三个查询会返回错误的结果,因为您只检查年份是否不同。请尝试以下

SELECT e.accounting_entity_id
FROM dc.pl_el_contingency e
where NOT EXISTS
              (select 1 from 
               dc.PL_ESCALATION s2
               where (e.fiscal_year = s2.fiscal_year)
               and  s2.fiscal_year IS not NULL)
 group by e.accounting_entity_id;