复杂sql-按【地点】变化分组(【地点停留】时长问题)

示例数据如下:

序号

时间

地点

1

9:00

A

2

9:01

A

3

9:03

A

4

9:04

B

5

9:09

A

要求结果:相邻时间地点相同的数据分为一组,不同则另分为一组。即序号1、2、3为一组,序号4为一组,序号5为一组。

参考结果:

序号

时间

地点

分组

1

9:00

A

1

2

9:01

A

1

3

9:03

A

1

4

9:04

B

2

5

9:09

A

3

参考实现:

WITH t as (
select 1 as id,'9:00' as date_time ,'A' as location union all
select 2 as id,'9:01' as date_time ,'A' as location union all
select 3 as id,'9:03' as date_time ,'A' as location union all
select 4 as id,'9:04' as date_time ,'B' as location union all
select 5 as id,'9:09' as date_time ,'A' as location 
)

select 
id
,date_time
,location
,per_location
,new_group_flag
,sum(new_group_flag) over(order by id) as group_id --最终分组id
from 
(
 select id,date_time,location,per_location,case when location=per_location then 0 else 1 end as new_group_flag from 
 (
   select id,date_time,location,lag(location,1) over(order by id) as per_location  from t
 ) t
) t1
;

+-----+------------+-----------+---------------+-----------------+-----------+--+
| id  | date_time  | location  | per_location  | new_group_flag  | group_id  |
+-----+------------+-----------+---------------+-----------------+-----------+--+
| 1   | 9:00       | A         | NULL          | 1               | 1         |
| 2   | 9:01       | A         | A             | 0               | 1         |
| 3   | 9:03       | A         | A             | 0               | 1         |
| 4   | 9:04       | B         | A             | 1               | 2         |
| 5   | 9:09       | A         | B             | 1               | 3         |
+-----+------------+-----------+---------------+-----------------+-----------+--+

猜你喜欢

转载自blog.csdn.net/cakecc2008/article/details/118679740