这是我使用datediff工作的SQL语句:
SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate FROM B134HREC WHERE datediff (dd, '2006-05-05', getdate()) > 90 GROUP BY b134_rmcid, b134_recdate ORDER BY b134_recdate DESC, b134_rmcid
我需要用MAX替换Hardcoded日期'2006-05-05'(b134_recdate),如下所示:
SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate FROM B134HREC where datediff (dd, MAX(b134_recdate), getdate()) > 90 GROUP BY b134_rmcid, b134_recdate ORDER BY b134_recdate DESC, b134_rmcid
但我收到此错误消息:
聚合可能不会出现在WHERE子句中,除非它位于HAVING子句或选择列表中包含的子查询中,并且要聚合的列是外部引用.
知道如何修复我的SQL语句吗?
尝试
SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate FROM B134HRE where datediff (dd, (select MAX(b134_recdate) from B134HRE) , getdate()) > 90 GROUP BY b134_rmcid, b134_recdate ORDER BY b134_recdate DESC, b134_rmcid