使用带有GROUP by的HAVING子句返回唯一记录

时间:2015-11-24 12:33:08

标签: sql sql-server tsql count sql-server-2014

美好的一天,

我很难理解GROUP BY AND HAVING子句的机制,并希望得到一些建议。

我正在尝试查询两个表 - 产品 ORDER_ITEMS PRODUCT_ID 列用于链接这两个表。

我希望查看从某个供应商订购的产品(使用 SUPPLIER_ID 列进行过滤,该列位于 ORDER_ITEMS );已成功订购( ORDER_STATUS 6 ORDER_ITEMS );且尚未删除( RECORD_DELETED ORDER_ITEMS )。我只使用 PRODUCTS 表来显示产品的名称。此外,我只想要退回不同的产品,这意味着我要排除任何与 PRODUCT_ID 列重复的结果

这是我正在使用的查询:

SELECT 
       PD.PRODUCT_ID,
       PD.PRODUCT_NAME,
       PD.BARCODE,
       PD.SUPPLIER_BARCODE,
       COUNT(PD.PRODUCT_ID) AS COUNTED,
       ODI.ORDER_ITEM_ID
FROM PRODUCTS PD
INNER JOIN ORDER_ITEMS ODI
ON PD.PRODUCT_ID = ODI.PRODUCT_ID
WHERE ODI.SUPPLIER_ID = 34359738399
  AND ORDER_STATUS = 6
  AND ODI.RECORD_DELETED = 0
GROUP BY PD.PRODUCT_ID,PD.PRODUCT_NAME,PD.BARCODE,PD.SUPPLIER_BARCODE,ODI.ORDER_ITEM_ID
HAVING COUNT(ODI.PRODUCT_ID) = 1
ORDER BY PRODUCT_ID ASC

不幸的是,这会返回 502 记录,其中许多记录会重复 PRODUCT_ID 。如果我从查询中移除 ORDER_ITEM_ID 175 ,则会返回记录。这175条记录是符合上述标准的产品。问题是我还需要从 ORDER_ITEMS (以及其他一些列)中提取 ORDER_ITEM_ID

我含糊地理解,当我包含 ORDER_ITEMS 时,查询会按 ORDER_ITEM 列对数据进行分组,因此会计算 PRODUCT_ID 基于每个 ORDER_ITEM_ID 的值。这导致每种产品的计数始终为1。

如何解决这个问题?另外,是否有更合适的方法来执行此任务,这样我就可以为每个重复的产品添加一个 ORDER_ITEM 记录?而不是像我上面那样完全省略它们?

这是上述查询返回的一些数据:

PRODUCT_ID,PRODUCT_NAME,BARCODE,SUPPLIER_BARCODE,COUNTED,ORDER_ITEM_ID

34359738628,ADCORTYL INTRA-ARTIC/DERMAL 10MG/ML 5ML,5099627022132,5012712000037,1,34359755708
34359739609,ARTELAC 3.2MG/ML EYE DROPS SOLN,5099627456722,5027519008933,1,34359741719
34359739626,ASACOLON 500MG SUPPOSITORIES,5099627516587,5015313012737,1,34359742783
34359739767,ATROVENT 250MCG/1ML UDV NEB SOLN,5099627639637,5012816012561,1,34359738421
34359739770,ATROVENT 500MCG/2ML UDV NEB SOLN,5099627460293,5012816012592,1,34359743524
34359739893,AZOPT 10MG/ML EYE DROPS SUSP,5099627831543,5015664002753,1,34359749091
34359739893,AZOPT 10MG/ML EYE DROPS SUSP,5099627831543,5015664002753,1,34359749687
34359739893,AZOPT 10MG/ML EYE DROPS SUSP,5099627831543,5015664002753,1,34359749715
34359739893,AZOPT 10MG/ML EYE DROPS SUSP,5099627831543,5015664002753,1,34359754053
34359740053,BACTIGRAS MED DRSS 10CMX10CM STERILE GMS,5099627672368,5000223421984,1,34359748101
34359740062,BACTROBAN 2% OINTMENT,5099627053914,5099211003165,1,34359755226
34359740558,BETNOVATE RD CREAM,5099627005692,5099211001642,1,34359752422
34359740558,BETNOVATE RD CREAM,5099627005692,5099211001642,1,34359738487
34359741045,BISODOL ANTACID TABS,5099627057707,5014398001438,1,34359750542
34359741995,BROLENE 0.1% EYE DROPS SOLN,5099627006323,50982790,1,34359746555
34359741995,BROLENE 0.1% EYE DROPS SOLN,5099627006323,50982790,1,34359751650
34359741995,BROLENE 0.1% EYE DROPS SOLN,5099627006323,50982790,1,34359751783
34359742132,BURINEX 1MG TABS,5099627551328,5702191004212,1,34359749705
34359742152,BUSCOPAN 20MG/ML SOLN FOR INJ,5099627006620,5012816018532,1,34359749083

在上面的示例中,返回了多条记录,其中包含重复的 PRODUCT_ID 值,例如ASACOLON 500MG SUPPOSITORIES

1 个答案:

答案 0 :(得分:2)

GROUP_CONCAT/LISTAGG中您需要SQL Server个等效项。您可以使用XML, STUFF and correlated subquery作为替代。

如果PRODUCT_IDUNIQUE,您可以使用:

WITH cte AS
(
  SELECT 
       PD.PRODUCT_ID,
       PD.PRODUCT_NAME,
       PD.BARCODE,
       PD.SUPPLIER_BARCODE,
       ODI.ORDER_ITEM_ID
  FROM PRODUCTS PD
  JOIN ORDER_ITEMS ODI
    ON PD.PRODUCT_ID = ODI.PRODUCT_ID
  WHERE ODI.SUPPLIER_ID = 34359738399
    AND ORDER_STATUS = 6
    AND ODI.RECORD_DELETED = 0
)
SELECT PRODUCT_ID,
       PRODUCT_NAME,
       BARCODE,
       SUPPLIER_BARCODE,
       [COUNTED] = COUNT(PD.PRODUCT_ID),
       [ORDER_ITEM_ID] = STUFF((SELECT CONCAT(',' , ORDER_ITEM_ID)
                                FROM cte c2
                                WHERE c2.PRODUCT_ID = c1.PRODUCT_ID
                                ORDER BY c2.ORDER_ITEM_ID
                                FOR XML PATH ('')), 1, 1, '')
FROM cte c1
GROUP BY PRODUCT_ID,PRODUCT_NAME,BARCODE,SUPPLIER_BARCODE
HAVING COUNT(PRODUCT_ID) = 1
ORDER BY PRODUCT_ID ASC;

LiveDemo_SimplifiedVersion

否则使用多列关联:

SELECT CONCAT(',' , ORDER_ITEM_ID)
FROM cte c2
WHERE c2.PRODUCT_ID = c1.PRODUCT_ID
  AND c2.PRODUCT_NAME = c1.PRODUCT_NAME
  AND ...
ORDER BY c2.ORDER_ITEM_ID
FOR XML PATH ('')), 1, 1, '')