gpt4 book ai didi

sql - SQL解决这个

转载 作者:行者123 更新时间:2023-12-03 19:50:28 24 4
gpt4 key购买 nike

我在称为TableA的表中包含以下数据:

ID  Status     Date
5 0 1000
20 0 900
10 1 800
30 1 700
4 1 600
8 0 500
22 1 400
1 1 300
3 0 200


记录按日期降序排列。我只想获取状态等于1的那些记录,但是直到状态不再为1的第一条记录。因此,在示例数据中,将选择ID为:10、30、4的记录,但选择22和出现1并不是因为ID 8出现并分隔了集合。最好将SQL运行在Sqlite中。此样本数据的结果应返回:

ID  Status     Date
10 1 800
30 1 700
4 1 600


编辑
我将ID值替换为随机值,并将日期从TEXT更改为Integer。

最佳答案

我建议

select * from tableA a1 where a1.status = 1 and not exists
(select 1 from tableA a2 where a2.status = 0 and a2.date > a1.date and a2.date <
(select max(date) from tableA a3 where a3.status = 1
)
)


双嵌套子查询。选择状态为1的行,其前面没有行(状态为0,且在最早的行为status为1之后)。

不知道这有多有效。

关于sql - SQL解决这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17101507/

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