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

Sql 寻找在select语句中执行是/否查询检查的最佳方法,sql,oracle,case,oracle-sqldeveloper,Sql,Oracle,Case,Oracle Sqldeveloper,我想知道是否有人能推荐最好的执行方法。我会向你介绍我正在做的工作。 我编写了一个select查询,其中包含一些获取订单记录的子查询,我有一些业务逻辑,这些订单需要满足这些逻辑,以便它们出现在报告中 此外,我还添加了一个嵌套的case语句,它可以帮助我确定是否满足业务逻辑,并简单地返回Yes或No。到目前为止,所有这些看起来都很棒! 例如。 以上只是一个订单29817的示例结果。接下来我需要做的是,当NOYESCHECK返回所有“是”时,只显示Order_No。 嵌套的Case语句: (case

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

此外,我还添加了一个嵌套的case语句,它可以帮助我确定是否满足业务逻辑,并简单地返回Yes或No。到目前为止,所有这些看起来都很棒! 例如。

以上只是一个订单29817的示例结果。接下来我需要做的是,当NOYESCHECK返回所有“是”时,只显示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运算符,但效果不太理想。我对所有操作员所做的尝试:

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)
它似乎只返回在我的支票中带有“是”的行,但这里的目的是: 如果对每个订单进行检查并返回至少一个“否”,则不显示订单。所以在上面的图片中,这个顺序并没有在我的查询结果中出现,但它确实出现了。所以我有点困了

任何帮助都将不胜感激。如果我需要提供更多信息,请告诉我

谢谢,
卡西亚

这有帮助吗?请参阅代码中的注释

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 check

Psuedo代码:

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'
    )
  )

Hi Kasia,请包括一个查询、一些最小表结构和预期结果,以便我们可以帮助您。更多信息是@wolφi这足够了还是需要更多?嘿@Littlefoot谢谢你现在就来试试这个!