Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Soma incorreta quando entro em uma segunda tabela


Uma maneira de evitar isso (sujeito ao suporte RDBMS) seria
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