选择查询中的SQL计数

时间:2017-11-28 08:39:25

标签: sql sql-server tsql

我有一个SQL查询,可以在购物公司的cab文件中创建标签。我想在订单中包含一定数量的包,有些包含多个。

我的select查询中的每个返回行都包含一个包含一个包但我需要计算它们。

所以我有:

Name  Email   Weight Price ID
Joe B J@.com    10   12.5. 1
Joe B J@.com    10   12.5. 1
Joe C JC@.com   10   14.5. 2

如何计算ID以返回名为pieces的列,在此示例中,ID为1,ID为2

由于

詹姆斯

    enter code here 

选择     'WPX'作为'产品代码',
     delivery_header.dh_datetime作为'发货日期',      'G'作为'装运类型',      order_header_detail.ohd_delivery_email作为'接收者电子邮件地址',   variant_detail.vad_weight作为'KG中的装运重量',

(很多其他领域......)

delivery_header.dh_number As 'Shippers Reference',
(SELECT Count(*)
FROM  delivery_header
WHERE
 dh_number = OU.dh_number
) As 'Number of Pieces',







From delivery_line_item Inner Join
  delivery_header On delivery_header.dh_id = delivery_line_item.dli_dh_id
  Inner Join
  order_line_item On delivery_line_item.dli_oli_id = order_line_item.oli_id
  Inner Join
  variant_detail On variant_detail.vad_id = order_line_item.oli_vad_id
  Inner Join
  order_header On order_header.oh_id = order_line_item.oli_oh_id Inner Join
  stock_location On stock_location.sl_id = order_line_item.oli_sl_id Inner Join
  customer_detail On customer_detail.cd_id = order_header.oh_cd_id Inner Join
  order_header_detail On order_header.oh_id = order_header_detail.ohd_oh_id
  Left Join
  order_header_analysis On order_header.oh_id = order_header_analysis.oha_oh_id
  Left Join
  order_customer_analysis On order_header.oh_id =
    order_customer_analysis.oca_oh_id Left Join
  order_delivery_analysis On order_header.oh_id =
    order_delivery_analysis.oda_oh_id Left Join
  order_line_analysis On order_line_item.oli_id = order_line_analysis.ola_oli_id
  Left Join
  order_line_product_analysis On order_line_item.oli_id =
    order_line_product_analysis.olpa_oli_id Left Join
  order_line_variant_analysis On order_line_item.oli_id =
    order_line_variant_analysis.olva_oli_id Inner Join
  product_detail On product_detail.pd_id = variant_detail.vad_pd_id Inner Join
  delivery_method On delivery_method.dm_id = order_header_detail.ohd_dm_id
  Inner Join
  delivery_method [Delivery Method] On [Delivery Method].dm_id =
    order_header_detail.ohd_dm_id Inner Join
  currency On currency.c_id = order_header.oh_c_id And currency.c_id =
    delivery_method.dm_c_id And currency.c_id = [Delivery Method].dm_c_id
Where 
  delivery_header.dh_number  IN (199364,199363,199362,199360)

Order By delivery_header.dh_number

2 个答案:

答案 0 :(得分:2)

您可以GROUP BY使用COUNT,如下所示:

SELECT ID, COUNT(*) as count FROM tbl GROUP BY ID

答案 1 :(得分:0)

我从您的问题中了解到,您正在寻找像

这样的输出
Name  Email   Weight Price ID Pieces 
Joe B J@.com    10   12.5. 1  2
Joe B J@.com    10   12.5. 1  2
Joe C JC@.com   10   14.5. 2  1

使用以下查询,您应该在大多数DBMS中获得所需的输出。

   SELECT NAME, 
       EMAIL, 
       WEIGHT, 
       PRICE, 
       ID, 
       (SELECT Count(*) 
        FROM   <YOUR_TABLE_NAME> 
        WHERE  ID= OU.ID) AS Pieces  
FROM   <YOUR_TABLE_NAME> OU

MS SQL Server的类似查询可以是

SELECT NAME, EMAIL,WEIGHT , Price, ID,CT.Pieces 
FROM <YOUR_TABLE_NAME> OU
CROSS APPLY 
(
  SELECT COUNT(*) AS Pieces FROM <YOUR_TABLE_NAME> IU WHERE OU.ID=IU.ID
 )CT