查询返回不正确的结果

时间:2020-04-05 22:19:52

标签: sql sql-server

我写了下面的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;

1 个答案:

答案 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;
相关问题