gpt4 book ai didi

mysql - 通过更改 bool 值进行分组

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

您好,我有以下 SQL 问题:

SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11

在我的表中有一个列filling_station_status。它可以是“免费”或“使用中”。

我想对元素进行分组,这样,如果filling_station_status发生更改(从“FREE”到“IN_USE”),它将在我的情况下创建一个日期范围,即date_created。在下一次从(“IN_USE”到“FREE”)的更改中,它会创建一个新的日期范围。

感谢您的建议。

最佳答案

如果您只需要 SQL 查询来生成输出中的日期范围,请尝试以下操作:

Select s.station_id, 
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)

关于mysql - 通过更改 bool 值进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23496982/

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