gpt4 book ai didi

MySQL 滞后函数

转载 作者:行者123 更新时间:2023-11-29 10:17:27 29 4
gpt4 key购买 nike

我正在编写代码以获得延迟和提前期。我能够成功获得正确的超前值,但获得错误的滞后值。下面是代码:

Select t1.user_id, t1.time_period,

t1.time_period - (select t2.time_period from monthly_usage as t2
where t2.user_id = t1.user_id and
t2.time_period < t1.time_period
order by t1.time_period desc LIMIT 1) as lag,
(SELECT
t2.time_period
FROM
monthly_usage as t2
WHERE
t2.user_id = t1.user_id
AND t2.time_period > t1.time_period
ORDER by t1.time_period
LIMIT 1) AS lead

from monthly_usage as t1;

我的输出是这样的

enter image description here

但我想要如下输出。

enter image description here

我的代码有错误吗?

最佳答案

让我更正您对滞后的查询

select t1.user_id, t1.time_period,
(select t2.time_period
from monthly_usage as t2
where t2.user_id = t1.user_id and
t2.time_period < t1.time_period
order by t2.time_period desc LIMIT 1) as lag, -- order by t2.time_period desc
....
from monthly_usage as t1;

关于MySQL 滞后函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921197/

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