From SQL to SPL: Generate event sequence numbers within a specified time interval

A certain table in the MS SQL database has three fields: account, date and time of string type.

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

Now we need to add a sequence number column Seq for the group. When a new event occurs for an account within one hour, Seq+1; If a new event occurs after one hour, reset Seq to 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

When referencing set members in relative positions, SQL needs to write tedious window functions and often involves multiple layers of nesting; The logical judgment of sequence number calculation is also quite complex, involving multiple layers of case when forms, and the code is very cumbersome. The sequence numbers need to be accumulated within a specified interval, which requires grouping these intervals or embedding window functions in case when, which can be very convoluted.

It is also possible to reference set members at relative positions in a loop and make logical judgments, but SQL does not support loop structures and requires the use of stored procedures, making the structure more complex.

SPL supports a complete procedural syntax, which can handle complex business logic in loops and conveniently reference set members using relative positions.


 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: Use JDBC to query the database, spell out the calculated column DT of date and time type, and sort it by account and DT.

A2: Create a new two-dimensional table and add a new calculated column Seq. When the account number of the current record is the same as the previous record and the time interval is within one hour, Seq+1; Otherwise, reset Seq to 1. [-1] represents the previous record of relative position.

Question sourcehttps://stackoverflow.com/questions/78380050/creating-sequence-numbers-with-hourly-reset