gpt4 book ai didi

mysql select sum, max, and another column value at the same row which the max value 停留

转载 作者:行者123 更新时间:2023-11-29 03:26:55 28 4
gpt4 key购买 nike

示例表:

uid time_stp                traf
1 2016-01-13 00:00:00 6
1 2016-01-13 05:00:00 8
1 2016-01-13 10:00:00 14
1 2016-01-13 15:00:00 26
1 2016-01-13 20:00:00 42
...

我的SQL:

select
uid,
from_unixtime(floor(unix_timestamp(time_stp)/3600) * 3600) as tm,
sum(traf),
max(traf),
-- xxx as max_traf_time
from
group_by_time_range
group by
uid, tm;

我想对每小时的traf进行求和,并在每个小时部分找到最大traf和对应的time_stp。我怎么可能用一条 SQL 语句来完成这些东西。

最佳答案

你试过吗

select
T1.uid,
from_unixtime(floor(unix_timestamp(time_stp)/3600) * 3600) as tm,
sum(traf),
(
SELECT max(traf)
FROM group_by_time_range T2
WHERE T2.uid = T1.uid
) as Max_Traf,
(
SELECT time_stp
FROM group_by_time_range T3
WHERE traf = Max_Traf
) as max_traf_time
from
group_by_time_range T1
group by
uid, tm;

关于mysql select sum, max, and another column value at the same row which the max value 停留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761883/

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