查询其他查询的输出中未包含的所有其余结果

时间:2019-01-17 19:52:34

标签: sql oracle

我在日志表中有订单,每个order_id可以有多个状态不同的行。我想返回具有辐射事件但状态不完整的订单列表。该表如下所示:

order_number | event | status | capture_id
-------------------------------------------
    1234     | fallout |complete | 95
    1234     | fallout |in progress| 95
    1234     | fallout |Assigned| 95
    1234     | task    |           | 
    2255     | fallout |in progress| 10
    2255     | fallout |Assigned| 10
    2255     | task |         | 

在此表中,查询将返回order_number 2255

我基于capture_id查询了一个列表,因为capture_id可以为同一order_number包含多个值。尽管在这种情况下为95和10,但与余波相对应的capture_id将在整个订单中保持不变。

SELECT* FROM RECORDS
WHERE event = 'fallout' and status  LIKE 'complete'
order by created desc;

^返回所有已完成的行

  SELECT * FROM  RECORDS
  WHERE capture_id NOT IN
   (SELECT capture_id FROM records
   WHERE event = 'fallout' and status  LIKE 'complete')
   order by created desc;

^这是我在查询目前未返回任何内容的capture_id其余部分时的尝试

1 个答案:

答案 0 :(得分:1)

  

^这是我尝试查询的capture_id的其余部分,目前未返回任何内容

使用正确的空值处理固定代码

SELECT *
FROM RECORDS
WHERE capture_id NOT IN
   (SELECT capture_id FROM records
   WHERE event = 'fallout' and status  LIKE 'complete' and capture_id IS NOT NULL)
order by created desc;

更多:Strange results from NOT IN subquery