列出所有行都准备好的订单

时间:2014-08-23 09:51:01

标签: sql sql-server

这是我的订单表。我想报告所有细节都准备好发货的所有订单。

orderNo detailNo    statusId    status  
10001   1           40        Ready For Shipment
10002   1           40        Ready For Shipment
10002   2           20        Canceled
10002   3           30        Pending
10003   1           40        Ready For Shipment
10003   2           40        Ready For Shipment
10004   1           10        New Order
10004   2           20        Canceled
10004   3           40        Ready For Shipment
10004   4           40        Ready For Shipment

预期结果如下:

Orders Ready For Shipment
10001 
10003

是否有任何有效的方法可以在不使用子查询的情况下实现就绪订单列表?

3 个答案:

答案 0 :(得分:1)

orderno分组并使用having仅获取没有其他状态的群组

select orderno
from your_table
group by orderno
having sum(case when status <> 'Ready For Shipment' then 1 end) = 0

或使用statusId

select orderno
from your_table
group by orderno
having sum(case when statusid <> 40 then 1 end) = 0

答案 1 :(得分:0)

select Distinct a.orderId 
from ordersTable a
inner join 
(
   select orderNo, Avg(statusId)
   from ordersTable
   group by orderNo
   having Avg(statusId) = 40) b
on a.orderNo = b.orderNo

答案 2 :(得分:-1)

你试过这个吗?

SELECT orderNo from <TABLE NAME> 
WHERE status="Ready For Shipment" ORDER BY orderNo