寻找在select语句中执行“是/否查询”检查的最佳方法

时间:2020-05-12 09:13:00

标签: sql oracle case oracle-sqldeveloper

我想知道是否有人可以推荐执行此操作的最佳方法。我将向您介绍我的工作。 我编写了一个带有一些子查询的选择查询,该查询获得了订单记录,我有一些订单需要满足的业务逻辑,以便它们出现在报告中。

此外,我还添加了嵌套案例陈述,它可以帮助我确定是否满足业务逻辑,并且仅返回是或否。到目前为止,一切看起来都不错! 例如。 enter image description here

以上只是一个订单的样本结果(29817)。接下来我需要做的只是在NOYESCHECK返回所有YES时仅显示Order_No。 嵌套的Case语句:

(case when sm.supply_code='Project Inventory' and 
  (select po.order_no 
        from purchase_order_line_all po 
        where po.contract = sm.contract
        and po.part_no = sm.part_no
        and po.activity_seq = sm.activity_seq 
        and po.project_id = sm.project_id
        and po.state in ('Closed','Arrived','Recieved') order by po.date_entered desc fetch first 1 row only) is not null then 'YES' 
  when sm.supply_code='Invent Order' and
        ( select sum(QTY_ONHAND - QTY_RESERVED)
        from inventory_part_in_stock ipis 
        where ipis.contract = sm.contract
        and ipis.part_no = sm.part_no
        and ipis.QTY_ONHAND - ipis.QTY_RESERVED > '0'
        and ipis.project_id  is null 
        and ipis.AVAILABILITY_CONTROL_ID not in ('QUARANTINE','RD','TRANSIT','PRE SCRAP')
        ) is not null then 'YES'

  else 'NO' end)NoYesCheck

实现此目标的最佳方法是什么?我尝试使用ALL运算符,但效果不如预期。我对ALL运算符尝试的操作:

and 'YES' = ALL (case when sm.supply_code='Project Inventory' and 
  (select po.order_no 
        from purchase_order_line_all po 
        where po.contract = sm.contract
        and po.part_no = sm.part_no
        and po.activity_seq = sm.activity_seq 
        and po.project_id = sm.project_id
        and po.state in ('Closed','Arrived','Recieved') order by po.date_entered desc fetch first 1 row only) is not null then 'YES' 
  when sm.supply_code='Invent Order' and
        ( select sum(QTY_ONHAND - QTY_RESERVED)
        from inventory_part_in_stock ipis 
        where ipis.contract = sm.contract
        and ipis.part_no = sm.part_no
        and ipis.QTY_ONHAND - ipis.QTY_RESERVED > '0'
        and ipis.AVAILABILITY_CONTROL_ID not in ('QUARANTINE','RD','TRANSIT','PRE SCRAP')
        and ipis.project_id  is null 
        ) is not null then 'YES'

  else 'NO' end)

它似乎只返回支票中带有“ YES”的行,但目的是: 如果对每个订单进行检查并返回至少一个“否”,则不显示订单。因此,在上图中,该顺序从没有被显示为查询的结果,但确实如此。所以我有点卡住了。

任何帮助将不胜感激。让我知道是否需要提供更多信息。

谢谢, 卡西亚

2 个答案:

答案 0 :(得分:1)

您可以在where子句中的子选择中使用NOYESCHECK列,并结合使用NOT IN检查。

伪代码:

select
  --main query columns
from data_source
where key_column not in (
  select distinct 
    key_column
  from (
    select
      key_column,
      noyescheck_column
    from data_source
    where noyescheck_column = 'NO'
    )
  )

答案 1 :(得分:0)

这会有所帮助吗?查看代码中的注释。

SQL> with
  2  -- this is what your query currently returns
  3  test (order_no, component_part, noyescheck) as
  4    (select 29817, 100, 'NO'  from dual union all
  5     select 29817, 101, 'YES' from dual union all
  6     --
  7     select 30000, 200, 'YES' from dual union all
  8     select 30000, 201, 'YES' from dual union all
  9     --
 10     select 40000, 300, 'NO'  from dual
 11    ),
 12  -- find ORDER_NOs whose NOYESCHECK = YES only
 13  yess as
 14    (select order_no
 15     from test
 16     group by order_no
 17     having min(noyescheck) = max(noyescheck)
 18        and min(noyescheck) = 'YES'
 19    )
 20  -- return only ORDER_NOs that satisfy condition
 21  select t.*
 22  from test t join yess y on y.order_no = t.order_no;

  ORDER_NO COMPONENT_PART NOY
---------- -------------- ---
     30000            200 YES
     30000            201 YES

SQL>