我有以下数据库表APP_USERS
USER_NAME OLD_STATUS NEW_STATUS STATUS_CHANGE_DATE ------------------------------------------------------------------------------ STEVE BALE LOGGED_OUT UNAVAILABLE 12/12/2012 5:04:24.736437 AM STEVE BALE UNAVAILABLE AVAILABLE 12/12/2012 6:04:24.736437 AM STEVE BALE AVAILABLE UNAVAILABLE 12/12/2012 7:31:08.591801 AM STEVE BALE UNAVAILABLE AVAILABLE 12/12/2012 7:41:46.373138 AM STEVE BALE AVAILABLE UNAVAILABLE 12/12/2012 8:30:21.218388 AM STEVE BALE UNAVAILABLE AVAILABLE 12/12/2012 9:24:27.812461 AM STEVE BALE AVAILABLE UNAVAILABLE 12/12/2012 10:44:52.724405 AM STEVE BALE UNAVAILABLE LOGGED_OUT 12/12/2012 11:30:50.724405 AM
我必须显示每个状态的用户状态开始日期和时间以及结束日期和时间。
类似于用户UNAVAILABLE从12/12/2012 5:04:24.736437 AM到12/12/2012 6:04:24.736437 AM。
答案 0 :(得分:3)
您只需要“下一个”记录,因为状态似乎正在发生变化。您可以使用主导功能执行此操作:
select user_name, new_status, status_change_date as StartTime,
lead(status_change_date) over (partition by user_name order by status_change_date) as EndTime
from app_users