根据一定条件分组并给出排名

时间:2017-10-06 12:15:38

标签: sql datetime

我想根据时间给出低于数据的排名(如果时间戳差小于15分钟,则排名等级+1) E.g

  • 时间:06:10:3​​2和06:12:38应该获得等级1
  • 时间:13:36:44和13:40:41以及13:44:47应该排名第2位
  • 并且最后4行应该获得等级3
   
user_id ride_id      createdat_local    
2681233 96783742    2017-10-04 06:10:32 
2681233 96784171    2017-10-04 06:12:38 
2681233 96924751    2017-10-04 13:36:44 
2681233 96925561    2017-10-04 13:40:41 
2681233 96926560    2017-10-04 13:44:47 
2681233 96994651    2017-10-04 18:12:29 
2681233 96995953    2017-10-04 18:18:16 
2681233 96996937    2017-10-04 18:22:15 
2681233 96997195    2017-10-04 18:24:00 

2 个答案:

答案 0 :(得分:2)

在SQL Server 2012+中:

使用common table expression中的窗口函数lag()datediff()createdat_local的上一行值进行比较,然后使用条件聚合进行sum() over()生成排名:

;with cte as (
select *
  , datediff(minute,lag(createdat_local) over (
      partition by user_id
      order by createdat_local
      ),createdat_local) as prev_dat
from t
)
select user_id, ride_id, createdat_local
  , sum(case when coalesce(prev_dat,16)>15 then 1 else 0 end) over (
    partition by user_id
    order by createdat_local
    ) as rank
from cte

rextester演示:http://rextester.com/EQUC48356

返回:

+---------+----------+---------------------+------+
| user_id | ride_id  |   createdat_local   | rank |
+---------+----------+---------------------+------+
| 2681233 | 96783742 | 2017-10-04 06:10:32 |    1 |
| 2681233 | 96784171 | 2017-10-04 06:12:38 |    1 |
| 2681233 | 96924751 | 2017-10-04 13:36:44 |    2 |
| 2681233 | 96925561 | 2017-10-04 13:40:41 |    2 |
| 2681233 | 96926560 | 2017-10-04 13:44:47 |    2 |
| 2681233 | 96994651 | 2017-10-04 18:12:29 |    3 |
| 2681233 | 96995953 | 2017-10-04 18:18:16 |    3 |
| 2681233 | 96996937 | 2017-10-04 18:22:15 |    3 |
| 2681233 | 96997195 | 2017-10-04 18:24:00 |    3 |
+---------+----------+---------------------+------+

答案 1 :(得分:0)

能够在redshift(psql)中获得所需的结果

查询: 与cte as( 选择 *, (DATEPART(' hour',createdat_local)* 60 + DATEPART('分钟',createdat_local)) - 延迟(DATEPART('小时',createdat_local)* 60 + DATEPART('分钟',createdat_local) )over(由createdat_local按user_id顺序划分)为diff_in_minutes 从T ) 选择user_id,ride_id,createdat_local   ,sum(合并时的情况(diff_in_minutes,16)> 15然后1其他0结束)结束(     按user_id分区     按无限制的前一行和当前行之间的createdat_local行排序     )作为排名 来自cte;