Per*_*ddy 1 sql t-sql sql-server sql-server-2008
我有以下查询:
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
Run Code Online (Sandbox Code Playgroud)
返回一个如下所示的结果集:
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
我怎么做到这一点?
谢谢!
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
Run Code Online (Sandbox Code Playgroud)
只需要删除box_id从GROUP BY和使用COUNT,为您在您的标题说.
| 归档时间: |
|
| 查看次数: |
173 次 |
| 最近记录: |