计算SQL Server结果

时间:2014-11-06 18:53:01

标签: sql-server

我希望COUNT(*) Offer表中有Product表中有多少行,如果Product.Producer表格中的123表中有Offer.PricePolicy列等于31# | ID ---------- 1 | 1277 2 | 1279 2 | 1280 列为3

此查询返回3个结果:

SELECT Product.ID
FROM Offer
LEFT JOIN Product ON Offer.ID = Product.ID
WHERE Product.Producer = 123
  AND Offer.PricePolicy = 31
GROUP BY Product.ID

如何修改它以返回结果数?

预期结果为{{1}}

{{1}}

1 个答案:

答案 0 :(得分:1)

只需将COUNT(*)添加到SELECT

即可
SELECT Product.ID, COUNT(*)
FROM Offer
LEFT JOIN Product ON Offer.ID = Product.ID
WHERE Product.Producer = 123
AND Offer.PricePolicy = 31
GROUP BY Product.ID

如果您不需要Product.ID,则投影中也只能有COUNT(*)