寻找执行 Yes/No 查询检查 select 语句的最佳方式

Looking for best way to execute Yes/No Query check in select statement

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

此外,我添加了一个 嵌套案例语句 ,它帮助我确定是否满足业务逻辑,它只是 return 是或否。到目前为止一切看起来都很棒! 例如。

以上只是一个订单 (29817) 的示例结果。我接下来需要做的只是在 NOYESCHECK return 全部为 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)

在我的检查中似乎 return 只包含 'YES' 行,但这里的目的是: 如果按订单进行检查并且 return 至少有一个 'No' 则不显示该订单。所以在上图中,这个订单从来没有打算作为结果出现在我的查询中,但它确实出现了。所以我有点卡住了。

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

谢谢, 卡西亚

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

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>

您可以在 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'
    )
  )