选择以下情况的陈述

时间:2018-08-15 18:12:19

标签: sql oracle

|   Column id         |     status       |
|          1          |      Accept      |
|          1          |     Shipped      |
|          1          |     Received     |
|          2          |      Accept      |
|          2          |     Received     |
|          3          |      Accept      |
|          3          |     Shipped      |
|          3          |     Received     |

我希望输出中的那些ID尚未发送但已经收到的ID。 (未发货但收货)(id 2)

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助:

select column_id
from table
where status = 'Received'
minus
select column_id
from table
where status = 'Shipped'

答案 1 :(得分:0)

这是获取id和相应的status

的另一种方法
select *
from
(select id ,listagg(status,',') within group(order by id) status_concat 
from table
group by id) CTE
where CTE.status_concat='Accept,Received';

如果不需要硬编码并且状态仅为这3个('Accept','Received','Shipped'),则下面的查询也将起作用

select id
from
 (select count(*) CNT,id  from table 
 group by id) CTE
where CTE.CNT=2;