如何从一个 table 打印 log_in 和 log_out 时间而没有任何用户 ID 和 sql 中 "on" 的计数

how to print log_in and log_out time from one table without any user id and count of "on" in sql

我有这个i/ptable

event_time status
10:01 on
10:02 on
10:03 on
10:04 off
10:07 on
10:08 on
10:09 off
10:11 on
10:12 off

我想要这个输出

login logout count_of_on
10:01 10:04 3
10:07 10:09 2
10:11 10:12 1

我尝试处理案例但没有帮助

在Oracle中,可以使用MATCH_RECOGNIZE做row-by-row处理:

SELECT *
FROM   table_name
MATCH_RECOGNIZE(
  ORDER BY event_time
  MEASURES
    FIRST(event_time)       AS login_time,
    LAST(event_time)        AS logout_time,
    COUNT(on_status.status) AS count_of_on
  PATTERN (on_status+ (off_status | $))
  DEFINE
    on_status AS status  = 'on',
    off_status AS status = 'off'
)

或者,在MySQL和Oracle中,都可以使用解析函数:

SELECT MIN(event_time) AS login_time,
       MAX(next_time)  AS logout_time,
       COUNT(*)        AS count_of_on
FROM   (
  SELECT t.*,
         LEAD(event_time, 1, event_time) OVER (ORDER BY event_time)
           AS next_time,
         ROW_NUMBER() OVER (ORDER BY event_time)
           - ROW_NUMBER() OVER (PARTITION BY status ORDER BY event_time) AS grp
  FROM   table_name t
) t
WHERE  status = 'on'
GROUP BY grp;

其中,对于示例数据:

CREATE TABLE table_name (event_time, status) AS
SELECT '10:01', 'on'  FROM DUAL UNION ALL
SELECT '10:02', 'on'  FROM DUAL UNION ALL
SELECT '10:03', 'on'  FROM DUAL UNION ALL
SELECT '10:04', 'off' FROM DUAL UNION ALL
SELECT '10:07', 'on'  FROM DUAL UNION ALL
SELECT '10:08', 'on'  FROM DUAL UNION ALL
SELECT '10:09', 'off' FROM DUAL UNION ALL
SELECT '10:11', 'on'  FROM DUAL UNION ALL
SELECT '10:12', 'off' FROM DUAL;

双输出:

LOGIN_TIME LOGOUT_TIME COUNT_OF_ON
10:01 10:04 3
10:07 10:09 2
10:11 10:12 1

db<>fiddle Oracle MySQL