我正在尝试选择一个布尔值,其中order.lastUpdated早于30分钟前,但我收到错误:
SQL状态[HY000];错误代码[1111];组功能使用无效; 嵌套异常是java.sql.SQLException:组的使用无效 功能
以下是查询:
select
c.externalReference channelReference,
c.id channelId,
max(o.lastUpdated) lastUpdated,
sum(if(max(o.lastUpdated) < date_sub(now(), interval 30 minute), 0, 1)) beforeThreshold
from channel c
join order_item o on c.id = o.channelId
where date(o.lastUpdated) = date(now())
and o.lastUpdated > date_sub(now(), interval 1 hour)
group by c.externalReference;
如果o.lastUpdated
早于30分钟前我必须使用max()
,我该如何返回布尔值?
答案 0 :(得分:1)
您无法嵌套聚合函数,例如SUM()
和MAX()
。你需要在子查询中做内部的。
SELECT c.externalReference AS channelReference,
c.id AS ChannelId
o.lastUpdated,
SUM(IF(o.lastUpdated < DATE_SUB(NOW(), INTERVAL 30 MINUTE), 0, 1) AS beforeThreshold
FROM channel AS c
JOIN (SELECT channelId, MAX(lastUpdated) AS lastUpdated
FROM order_item
WHERE DATE(lastUpdated) = TODAY()
AND lastUpdated > DATE_SUB(NOW(), INTERVAL 1 HOUR)
GROUP BY channelId) AS o
ON c.id = o.channelId
GROUP BY c.externalReference