gpt4 book ai didi

mysql 计数(SUM CASE)与同一张表上的连接

转载 作者:行者123 更新时间:2023-11-29 05:05:30 25 4
gpt4 key购买 nike

我有一个查询,我需要比较同一个表中的 2 行(不连续),其中一行的 ID 必须小于另一行,并且字段名称必须等于特定值。如果我不包含连接,我可以提取数据,但是当我尝试连接数据时,我得到“列 e1 未知”。我假设它与计数有关,但我似乎无法修复它。有什么想法吗?

SELECT 
case
when ( extract( HOUR FROM e1.created_at) = 0 ) then '12am'
when ( extract( HOUR FROM e1.created_at) < 12) then concat(extract(HOUR FROM e1.created_at), 'am')
when ( extract( HOUR FROM e1.created_at) = 12) then '12pm'
when ( extract( HOUR FROM e1.created_at) > 12) then concat( ( extract(HOUR FROM e1.created_at) -12 ), 'pm')
end AS "Time",
sum(case when e1.result = “result1" and e2.result =“result2" and e1 < e2 then 1 else 0 end) AS ‘My Result'
from event e1
join event e2 on e2.secondary_id = e1.secondary_id
where e1.started_at > '2018-02-19 00:00:00' and e1.started_at < '2018-02-20 00:00:00'
group by extract(hour from e1.created_at)

最佳答案

因为 e1e2表别名,所以这个 CASE 表达式是不正确的:

e1 < e2

您需要指定要比较的 event 表中的哪些字段。假设您要比较时间戳,sum 表达式应如下所示:

sum(case when e1.result = “result1" and e2.result =“result2" and e1.started_at < e2.started_at then 1 else 0 end) AS ‘My Result'

关于mysql 计数(SUM CASE)与同一张表上的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49009221/

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