Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server查询问题(可能计数?)_Sql_Sql Server_Tsql_Sql Server 2008 - Fatal编程技术网

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