I have a tricky query design requirement, i have tried different types/different combination of Analytical function to achieve my result from the below data set. My other plan is do write stored proc, however i want to reach out to the expert group before i change my direction.
Input Data Set:
Required Output Data Set with the Group Column: When there is a session id change in the session id and if i get back the same session id again, i have to have a different group to it. I tried to use LEAD/LAG combination, however unable to get the below desired output, one or other scenario was breaking.
Thanks !
SQL language is expressive enough to find a declarative solution for complex requirements.
Snowflake has recently implemented SQL 2016 Standard clause: MATCH_RECOGNIZE, which was designed to solve such cases in very straighforward way.
Data preparation:
Query:
db<>fiddle demo(Oracle)
Output:
How it works:
(a+b*)
which is Perl-style regexp, a(one or more) b(zero or more)MATCH_NUMBER()
-"Returns the sequential number of the match"POL_ID
and useVERSION_ID
as sorting column