SQL Server查询问题(可能计数?)
我有以下疑问:SQL Server查询问题(可能计数?),sql,sql-server,tsql,sql-server-2008,Sql,Sql Server,Tsql,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 返回的结果集如下所示: 装运id、箱子id、装运状态 101号
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
返回的结果集如下所示:
装运id、箱子id、装运状态
101号,boxA,2号
101号,B区,2号
101号,boxC,2号
第二信箱101号102室
第102号信箱,2号
第二栏A1第103号
第二栏第103页
我想返回这样的内容,而不是显示每次装运的箱子总数:
装运标识、箱数、装运状态
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
正如您在标题中所说的,只需从组中删除box_id并使用COUNT即可
只需从组中删除框id,然后使用COUNT,如您在标题中所述。尝试以下操作:
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
试试这个:
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