我有两个表,我需要比较它的id并且只对具有相似id的行求和。 因此,我需要显示日期,ID,总和(对于每个id)
我目前的代码是
Select a.OraReg,
a.IdMezzo ,
b.OraReg,
b.IdMezzo,
SUM(Saliti) over() as totalCount
from FLASH_STORICO.dbo.D_MSG_CONTAPASS as a
inner join FLASH_STORICO.dbo.D_MSG_BORDO as b
on a.IdMezzo =b.IdMezzo
and a.OraReg = b.OraReg
where a.OraReg > DATEADD(hour, -1, SYSDATETIMEOFFSET())
order by a.IdMezzo
我的当前结果显示每个ID的相同金额,
我做错了什么?
答案 0 :(得分:0)
试试这个,
SELECT a.OraReg
,a.IdMezzo
,SUM(Saliti) AS SumOfSaliti
FROM FLASH_STORICO.dbo.D_MSG_CONTAPASS a
INNER JOIN FLASH_STORICO.dbo.D_MSG_BORDO b ON a.IdMezzo = b.IdMezzo
AND a.OraReg = b.OraReg
WHERE
AND a.OraReg > DATEADD(hour, - 1, SYSDATETIMEOFFSET())
GROUP BY a.OraReg
,a.IdMezzo
ORDER BY a.IdMezzo
答案 1 :(得分:0)
尝试
SELECT a.OraReg ,a.IdMezzo ,b.OraReg ,b.IdMezzo ,SUM(Saliti) AS totalCount
FROM FLASH_STORICO.dbo.D_MSG_CONTAPASS AS a
JOIN FLASH_STORICO.dbo.D_MSG_BORDO AS b ON a.IdMezzo = b.IdMezzo AND a.OraReg = b.OraReg
WHERE a.OraReg > DATEADD(hour, - 1, SYSDATETIMEOFFSET())
GROUP BY a.OraReg,a.IdMezzo,b.OraReg,b.IdMezzo
ORDER BY a.IdMezzo
如果这不起作用,请遵循@GuidoG建议。