所以我最近在一个问题上得到了很好的帮助。但是,我需要更精确一些,希望可以在SQL中实现。
这是我的最后一个问题:
我的细微差别是:
personID | status | unixtime | column d | column e | column f
1 2 213214 x y z
1 2 213325 x y z
1 2 213326 x y z
1 2 213327 x y z
1 2 213328 x y z <-- I want this
1 3 214330 x y z <-- Any of this is OK
1 3 214331 x y z
1 3 214332 x y z <-- I want this or
1 2 324543 x y z <-- I want this
因此,我要不是岛的起点,而是要岛的终点。如果我介于两者之间,那完全可以,最好是到此为止。但是我真的希望状态改变的“在...之前”和“在...之后”是什么,如果有任何意义的话。这可能是特定状态。
select t.*
from (select t.*,
case when status <> lag(status,1,NULL) over(partition by personID order by unixtime)
then 1
when lag(status,1,NULL) over(partition by personID order by unixtime) is null
then 1
else 0 end as start_status,
case when status <> lead(status,1,NULL) over(partition by personID order by unixtime)
then 1
when lead(status,1,NULL) over(partition by personID order by unixtime) is null
then 1
else 0 end as end_status
from mytable t
) t
where end_status = 1
--or start_status = 1 -- uncomment this line if you want start statuses as well
此查询将产生所有终止或启动分区的行(或在单行分区的情况下都行):
SELECT *
FROM (
SELECT *
, lag(status) OVER w IS DISTINCT FROM status AS partition_start
, lead(status) OVER w IS DISTINCT FROM status AS partition_end
FROM tbl
WINDOW w AS (PARTITION BY personID ORDER BY unixtime)
) sub
WHERE (partition_start OR partition_end)
ORDER BY personID, unixtime;
db <>小提琴here