gpt4 book ai didi

mysql - 选择在
转载 作者:行者123 更新时间:2023-11-29 16:24:05 24 4
gpt4 key购买 nike

我有以下数据。

KEY     |ERRRTYPE       |CREATEDATE |ERROR  
101aa |Not on Target |1/2/2019 |Y
111bw |Not on Target |1/2/2019 |Y
123ds |Not on Target |1/2/2019 |Y
101aa |Not on Target |1/3/2019 |Y
121kd |Not on Target |1/3/2019 |Y
111bw |Not on Target |1/3/2019 |Y
132ee |Not on Target |1/4/2019 |Y
123ww |Not on Target |1/4/2019 |Y
111bw |Not on Target |1/4/2019 |Y
123kk |Not on Target |1/4/2019 |Y
111bw |Not on Target |1/5/2019 |Y
101aa |Not on Target |1/5/2019 |Y
234ew |Not on Target |1/5/2019 |Y
111bw |Not on Target |1/6/2019 |Y
101aa |Not on Target |1/6/2019 |Y
124dd |Not on Target |1/6/2019 |Y

我需要找到拉取当前或最新日期记录,以及如果 KEY 字段在过去 5 天或更短时间内连续出现,则需要在新列中返回 Y,

以下数据应如下所示:

KEY     |ERRRTYPE       |CREATEDATE     |ERROR  |Consecutive 
111bw |Not on Target |1/6/2019 |Y | Y
101aa |Not on Target |1/6/2019 |Y | Y
124dd |Not on Target |1/6/2019 |Y | N

由于 KEYs = 111bw 每天连续重复 5 次,并且 KEY 101aa 连续重复 2 次,连续错误字段应为 Y,否则为 N

最佳答案

以下解决方案是在 SQL Server 上准备的我不确定它是否可以在 MySQL 上成功运行由于所有函数都是标准 SQL,如 dense_rank ,CTE等,我希望有一种方法可以将以下代码转换为MySQL代码或者按原样工作

;with cte as (
select
*, dense_rank() over (order by createdate desc) rn
from ErrorData
)
select
[KEY], ERRRTYPE, CREATEDATE, ERROR,
case when t.cnt = 5 then 'Y' else 'N' end as Consecutive
from cte
cross apply (
select
count(*) as cnt
from ErrorData e
where e.[KEY] = cte.[KEY]
and e.createdate >= dateadd(dd, -5, cte.createdate)
) as t
where rn = 1

enter image description here

关于mysql - 选择在 <x 天的时间段内继续重复发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359187/

24 4 0

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