SQL,生成指定时间间隔内的事件次序号
MSSQL 数据库某表有三个字段:账号、字符串类型的日期和时间。
Account_Number |
FuelPurchase_Date |
Fuel_TOD |
19 |
2024-04-03 |
07:02:02 AM |
19 |
2024-04-03 |
07:02:41 AM |
19 |
2024-04-03 |
02:58:49 PM |
19 |
2024-04-03 |
07:58:49 PM |
19 |
2024-04-05 |
02:58:49 PM |
19 |
2024-04-05 |
02:59:31 PM |
19 |
2024-04-17 |
11:56:13 PM |
20 |
2024-04-17 |
11:59:13 PM |
19 |
2024-04-18 |
12:15:13 AM |
19 |
2024-04-18 |
02:56:13 PM |
20 |
2024-04-18 |
07:41:55 AM |
20 |
2024-04-18 |
07:41:55 PM |
20 |
2024-04-18 |
07:56:55 PM |
19 |
2024-04-19 |
07:41:55 AM |
19 |
2024-04-19 |
07:42:20 AM |
19 |
2024-04-19 |
08:41:20 AM |
现在要新增一个分组的序号列 Seq,当某个账号在一个小时内发生新事件时,Seq+1;如果一个小时后才发生新事件,则重置 Seq 为 1。
Account_Number |
FuelPurchase_Date |
Fuel_TOD |
Seq |
19 |
2024-04-03 |
07:02:02 AM |
1 |
19 |
2024-04-03 |
07:02:41 AM |
2 |
19 |
2024-04-03 |
02:58:49 PM |
1 |
19 |
2024-04-03 |
07:58:49 PM |
1 |
19 |
2024-04-05 |
02:58:49 PM |
1 |
19 |
2024-04-05 |
02:59:31 PM |
2 |
19 |
2024-04-17 |
11:56:13 PM |
1 |
19 |
2024-04-18 |
12:15:13 AM |
2 |
19 |
2024-04-18 |
02:56:13 PM |
1 |
19 |
2024-04-19 |
07:41:55 AM |
1 |
19 |
2024-04-19 |
07:42:20 AM |
2 |
19 |
2024-04-19 |
08:41:20 AM |
3 |
20 |
2024-04-17 |
11:59:13 PM |
1 |
20 |
2024-04-18 |
07:41:55 AM |
1 |
20 |
2024-04-18 |
07:41:55 PM |
1 |
20 |
2024-04-18 |
07:56:55 PM |
2 |
编写SPL代码
A |
|
1 |
=mssql.query("select *,cast(FuelPurchase_Date as datetime) + cast(Fuel_TOD as datetime) as DT from tb order by Account_Number,DT”) |
2 |
=A1.new(Account_Number,FuelPurchase_Date,Fuel_TOD, if(Account_Number==Account_Number[-1] && interval@s(Fuel_DT[-1],Fuel_DT)<3600,Seq[-1]+1,1):Seq) |
A1:通过JDBC查询数据库,拼出日期时间类型的计算列DT,并按账号和DT排序。
A2:新建二维表,增加新计算列Seq。当前记录的账号与上一条记录相同,且时间间隔一小时内时,Seq+1;否则将Seq重置为1。[-1]表示相对位置的上一条记录。
问题来源:https://stackoverflow.com/questions/78380050/creating-sequence-numbers-with-hourly-reset
英文版 https://c.scudata.com/article/1732785350785