需求是首先计算出两条记录的时间差,然后再根据这个时间差的大小来决定最终的值(例如大于12个小时就为1,否则为0);
那么语句大致为
select TIMESTAMPDIFF(hour,min(time),max(time)) as time_hour,
CASE
when (time_hour mod 24) > 12 then (time_hour div 24 )+1
else (time_hour div 24 )
end as time_day
from …….
但是这样是编译不过的,提示是time_hour是未知列,假如本人把TIMESTAMPDIFF(hour,min(time),max(time)) 这整个替换case里的time_hour,那应该是可以的,问题是这样是不是就会造成多次计算TIMESTAMPDIFF(hour,min(time),max(time)) ,有没有办法可以指计算一次,然后后面多次使用?
那么语句大致为
select TIMESTAMPDIFF(hour,min(time),max(time)) as time_hour,
CASE
when (time_hour mod 24) > 12 then (time_hour div 24 )+1
else (time_hour div 24 )
end as time_day
from …….
但是这样是编译不过的,提示是time_hour是未知列,假如本人把TIMESTAMPDIFF(hour,min(time),max(time)) 这整个替换case里的time_hour,那应该是可以的,问题是这样是不是就会造成多次计算TIMESTAMPDIFF(hour,min(time),max(time)) ,有没有办法可以指计算一次,然后后面多次使用?
解决方案
40
SELECT CASE
when (time_hour mod 24) > 12 then (time_hour div 24 )+1
else (time_hour div 24 )
end as time_day
FROM (
select TIMESTAMPDIFF(hour,min(time),max(time)) as time_hour
from ……)t1
改为嵌套,具体还可以微调
when (time_hour mod 24) > 12 then (time_hour div 24 )+1
else (time_hour div 24 )
end as time_day
FROM (
select TIMESTAMPDIFF(hour,min(time),max(time)) as time_hour
from ……)t1
改为嵌套,具体还可以微调