SQL 通过多次操作选择已完成的订单
我有一个包含子操作的订单列表。 如何创建已完成订单列表? 已完成的订单必须已完成所有子操作。
表“订单”:
order_no | suboperation | finished
1 | preparing | 01/01/2009
1 | scrubbing | 01/05/2009
1 | painting | 01/10/2009
2 | preparing | 02/05/09
2 | painting | NULL
3 | preparing | 03/01/2009
3 | scrubbing | 03/15/2009
3 | painting | 03/10/2009
4 | bending | NULL
4 | crashing | NULL
4 | staining | NULL
4 | painting | NULL
期望的输出(已完成的订单):
order_no
1
3
I have a list of orders with suboperations. How can I create a list of finished orders? Finished order must have finished all suboperations.
Table "orders":
order_no | suboperation | finished
1 | preparing | 01/01/2009
1 | scrubbing | 01/05/2009
1 | painting | 01/10/2009
2 | preparing | 02/05/09
2 | painting | NULL
3 | preparing | 03/01/2009
3 | scrubbing | 03/15/2009
3 | painting | 03/10/2009
4 | bending | NULL
4 | crashing | NULL
4 | staining | NULL
4 | painting | NULL
Desired output (finished orders):
order_no
1
3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用计数、分组依据和拥有。 这避免了必须执行任何更有效的表连接。
You'll could also use count, group by and having. This avoids having to do any table joins which is more efficient.
一个好的
WHERE NOT EXISTS
子句应该在这里工作:A good ol'
WHERE NOT EXISTS
clause ought to work here:我试图找到一种使用 MIN 函数的方法,并想出了这个:
SELECT order_no
FROM Orders
GROUP BY order_no
HAVING (MIN(ISNULL(finished, 0)) <> 0)
null 可能很烦人......
也许性能不高,但对我来说更容易理解。
I was trying to find a way to use the MIN function, and came up with this:
SELECT order_no
FROM Orders
GROUP BY order_no
HAVING (MIN(ISNULL(finished, 0)) <> 0)
null can be annoying...
not as performant, maybe, but easier for me to understand.