MariaDB:SQL 删除不同时间戳之间的重复项
MariaDB: SQL to remove duplicates between different timestamps
我有以下 table 并且想要删除最旧和最新时间戳之间的重复项。
timestamp
type
state
2019-10-01 04:51:42
mod
off
2019-10-01 04:54:42
mod
off
2019-10-01 04:56:42
mod
off
2019-10-01 05:01:42
mod
on
2019-10-01 05:21:42
mod
on
2019-10-01 05:30:42
mod
on
2019-10-01 05:43:42
mod
off
2019-10-01 05:51:42
mod
off
2019-10-01 06:02:42
mod
off
2019-10-01 06:05:42
mod
off
2019-10-01 06:10:42
mod
on
2019-10-01 06:15:42
mod
on
2019-10-01 06:25:42
mod
on
这是我想要的通用 SQL 语句的结果。如果状态更改,我需要第一个时间戳。
timestamp
type
state
2019-10-01 04:51:42
mod
off
2019-10-01 05:01:42
mod
on
2019-10-01 05:43:42
mod
off
2019-10-01 06:10:42
mod
on
感谢您的帮助:-)
看到这个DBFIDDLE
SELECT `timestamp`,type,state
FROM (
SELECT
`timestamp`,
type,
state,
LEAD(state) OVER (ORDER BY `timestamp`) as Next,
LAG(state) OVER (ORDER BY `timestamp`) as Previous
FROM t1
) x
WHERE state=Next AND (state<>Previous OR Previous is null);
我有以下 table 并且想要删除最旧和最新时间戳之间的重复项。
timestamp | type | state |
---|---|---|
2019-10-01 04:51:42 | mod | off |
2019-10-01 04:54:42 | mod | off |
2019-10-01 04:56:42 | mod | off |
2019-10-01 05:01:42 | mod | on |
2019-10-01 05:21:42 | mod | on |
2019-10-01 05:30:42 | mod | on |
2019-10-01 05:43:42 | mod | off |
2019-10-01 05:51:42 | mod | off |
2019-10-01 06:02:42 | mod | off |
2019-10-01 06:05:42 | mod | off |
2019-10-01 06:10:42 | mod | on |
2019-10-01 06:15:42 | mod | on |
2019-10-01 06:25:42 | mod | on |
这是我想要的通用 SQL 语句的结果。如果状态更改,我需要第一个时间戳。
timestamp | type | state |
---|---|---|
2019-10-01 04:51:42 | mod | off |
2019-10-01 05:01:42 | mod | on |
2019-10-01 05:43:42 | mod | off |
2019-10-01 06:10:42 | mod | on |
感谢您的帮助:-)
看到这个DBFIDDLE
SELECT `timestamp`,type,state
FROM (
SELECT
`timestamp`,
type,
state,
LEAD(state) OVER (ORDER BY `timestamp`) as Next,
LAG(state) OVER (ORDER BY `timestamp`) as Previous
FROM t1
) x
WHERE state=Next AND (state<>Previous OR Previous is null);