SQL每次都会按时间段选择用户的状态更改
我正在尝试从表中检索信息,该表看起来
"ID" "Type" "Status" "LAST_UPDATE"
"123456" "one" "ACTIVE" 12/01/22
"123456" "one" "DISABLED" 16/01/22
"123456" "one" "ACTIVE" 19/01/22
"234567" "one" "ACTIVE" 01/02/22
"345678" "two" "DISABLED" 04/03/22
我想在每次状态更改时选择last_update范围以检索如下信息:
ID date_range Status
123456 12/01/22 - 16/01/22 "ACTIVE"
123456 16/01/22 - 19/01/22 "DISABLED"
对于每个ID。
我成功执行的唯一步骤是通过以下方式获取每个用户的 last_update 状态:
SELECT a.* FROM user_table a WHERE a.LAST_UPDATE = (SELECT MAX(b.LAST_UPDATE)
FROM user_table b
WHERE b.id = a.id
ORDER BY b.id DESC
LIMIT 1)
I'm trying to retrieve information from a table which looks like
"ID" "Type" "Status" "LAST_UPDATE"
"123456" "one" "ACTIVE" 12/01/22
"123456" "one" "DISABLED" 16/01/22
"123456" "one" "ACTIVE" 19/01/22
"234567" "one" "ACTIVE" 01/02/22
"345678" "two" "DISABLED" 04/03/22
I want to select last_update ranges everytime the status changes to retrieve information like:
ID date_range Status
123456 12/01/22 - 16/01/22 "ACTIVE"
123456 16/01/22 - 19/01/22 "DISABLED"
For every ID.
The only step I succeeded to do is to get the status for every user last_update with:
SELECT a.* FROM user_table a WHERE a.LAST_UPDATE = (SELECT MAX(b.LAST_UPDATE)
FROM user_table b
WHERE b.id = a.id
ORDER BY b.id DESC
LIMIT 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里使用它,您可以获得所需的数据。如果您不想进入零条件,请添加条件。
Using it here you get the data you want. If you do not want null entry then please add condition.