我写了下面的SQL查询来提取特定产品ID(500)的收入,但是它返回9335个结果,但都相同(重复)。
DECLARE @ProductID char(3) = '500'
SELECT
P.ProductID,
P.Name,
P.Price,
S.Quantity,
CAST(Price * Quantity AS float) AS Revenue
FROM
Sales S
INNER JOIN
Products P ON S.ProductID = P.ProductID
WHERE
p.ProductID = @ProductID;
答案 0 :(得分:1)
大概是您想要的聚合:
SELECT P.ProductID, P.Name, P.Price,
SUM(Price*Quantity) as Revenue
FROM Sales S INNER JOIN
Products P
ON S.ProductID = P.ProductID
WHERE p.ProductID = @ProductID
GROUP BY P.ProductID, P.Name, P.Price;