我有一个ticket_events表,其中显示了签入和签出时间戳。
timestamp type
2018-12-26 02:46:01 IN
2018-12-26 02:46:13 IN
2018-12-26 03:17:57 OUT
2018-12-26 04:12:13 IN
2018-12-26 04:12:25 IN
2018-12-26 04:35:41 OUT
2018-12-26 04:35:53 OUT
2018-12-26 05:11:05 IN
2018-12-26 06:03:42 OUT
2018-12-26 06:03:55 OUT
2018-12-26 06:04:43 IN
2018-12-26 06:04:50 IN
2018-12-26 06:28:07 OUT
2018-12-26 06:28:19 OUT
2018-12-26 07:00:11 IN
2018-12-26 07:00:22 IN
2018-12-26 07:19:13 OUT
2018-12-26 07:59:21 IN
2018-12-26 08:24:57 OUT
我只希望表中出现IN OUT对,而没有连续的IN或OUT。例如,我想要:
timestamp type
2018-12-26 02:46:01 IN
2018-12-26 03:17:57 OUT
2018-12-26 04:12:13 IN
2018-12-26 04:35:41 OUT
2018-12-26 05:11:05 IN
2018-12-26 06:03:42 OUT
2018-12-26 06:04:43 IN
2018-12-26 06:28:07 OUT
2018-12-26 07:00:11 IN
2018-12-26 07:19:13 OUT
2018-12-26 07:59:21 IN
2018-12-26 08:24:57 OUT
有人知道要执行此操作的sql查询吗?
我现在的查询是
select timestamp, type from ticket_events where ticket_uuid='value'
答案 0 :(得分:2)
您要删除上一行具有相同类型的任何行。所以:
select timestamp, type
from (select t.*,
lag(type) over (order by timestamp) as prev_type
from ticket_events t
) t
where prev_type <> type or prev_type is null;
where
子句也可以表述为:
where prev_type is distinct from type
如果要删除“违规”行,可以执行以下操作-假设timestamp
是唯一的:
delete from ticket_events
using (select t.*,
lag(type) over (order by timestamp) as prev_type
from ticket_events t
) tt
where tt.timestamp = t.timestamp and
tt.prev_type = t.type;
答案 1 :(得分:1)
这是一个差距和孤岛的问题。
您可以使用ROW_NUMBER
窗口函数来获取间隙,然后将group by
与min
一起使用
SELECT MIN(timestamp) "timestamp",
type
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY timestamp)-
ROW_NUMBER() OVER(PARTITION BY type ORDER BY timestamp) gap
FROM ticket_events
where ticket_uuid='value'
) t1
GROUP BY gap,type
ORDER BY MIN(timestamp)
Results :
| timestamp | type |
|----------------------|------|
| 2018-12-26T02:46:01Z | IN |
| 2018-12-26T03:17:57Z | OUT |
| 2018-12-26T04:12:13Z | IN |
| 2018-12-26T04:35:41Z | OUT |
| 2018-12-26T05:11:05Z | IN |
| 2018-12-26T06:03:42Z | OUT |
| 2018-12-26T06:04:43Z | IN |
| 2018-12-26T06:28:07Z | OUT |
| 2018-12-26T07:00:11Z | IN |
| 2018-12-26T07:19:13Z | OUT |
| 2018-12-26T07:59:21Z | IN |
| 2018-12-26T08:24:57Z | OUT |