sql >> Base de Datos >  >> RDS >> Sqlserver

Suma incorrecta cuando me uno a una segunda mesa

Una forma de evitar esto (sujeto al soporte de RDBMS) sería

WITH R
     AS (SELECT *,
                Sum(HeadCount) OVER (PARTITION BY date) AS SumHeadCount
         FROM   Report)
SELECT R.date,
       SumHeadCount,
       Sum(P.Quantity) AS SumQuantity
FROM   R
       JOIN Production P
         ON R.ReportID = P.ReportID
GROUP  BY R.date, SumHeadCount
ORDER  BY R.date