gpt4 book ai didi

sql - 在 MySQL 语句中使用 datediff

转载 作者:行者123 更新时间:2023-12-01 00:34:13 25 4
gpt4 key购买 nike

这是我使用 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(b134_recdate) 替换硬编码日期“2006-05-05”:

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

但我收到此错误消息:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

知道如何修复我的 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

关于sql - 在 MySQL 语句中使用 datediff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/689819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com