DEV Community

Judy
Judy

Posted on

SQL: Generate event sequence numbers within a specified time interval #eg89

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

Image description
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.

Image description
SPL code:

Image description
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.

Open source SPL source address

Free Download

Top comments (0)