我有以下查询:
SELECT A.shipment_id
,B.box_id
,A.shipment_status
FROM shipments A
join boxes B on A.shipment_id = B.shipment_id
where A.shipment_status = 2
Group by B.box_id, A.shipment_id, A.shipment_status
返回一个如下所示的结果集:
shipment_id , box_id , shipment_status
101,boxA,2
101,boxB,2
101,boxC,2
102,box101,2
102,box102,2
103,boxA1,2
103,boxA2,2
我想返回这样的东西(显示每次发货的总箱数):
shipment_id ,包装箱数, shipment_status
101,3,2
102,2,2
103,2,2
我将如何实现这一目标?
谢谢!
答案 0 :(得分:8)
SELECT A.shipment_id
,COUNT(*) AS boxcount
,A.shipment_status
FROM shipments A
join boxes B on A.shipment_id = B.shipment_id
where A.shipment_status = 2
Group by A.shipment_id, A.shipment_status
只需要从box_id
中移除GROUP BY
并使用COUNT
,就像您在标题中所说的那样。
答案 1 :(得分:0)
试试这个:
SELECT A.shipment_id
, count(1)
, A.shipment_status
FROM shipments A
join boxes B on A.shipment_id = B.shipment_id
where A.shipment_status = 2
Group by A.shipment_id, A.shipment_status