Skip to content
Advertisement

Partitioning rows into groups by accumulative time interval

I got a search sessions log that looks like this:

My task is to partition each row into session groups. Session groups are up to five minutes.

For example:

Those TOP 3 sessions will form a group session 1 – if we accumulate the minutes between each row, we will get 3 minutes and the 4th would accumulate to more then 5 minutes so it will be a different session group.

I manipulated the table like this in order to get it ready for partitioning:

And got this:

I thought about two possibilities to continue:

  1. Using a window function, like RANK(), I can partition the rows, but I can’t figure out how to set the PARTITION BY caluse with a condition to do so.

  2. To iterate the table with a WHILE loop – again finding hard time to form ths

Advertisement

Answer

This cannot be done with just window functions. You need some kind of iterative process, that keeps track of the first row of each group, and dynamically identififes the next one.

In SQL, you can phrase this with a recursive query:

For your sample data, this returns:

dt         | search_time             | searches | rn | first_search_time       | grp
:--------- | :---------------------- | -------: | -: | :---------------------- | --:
2020-06-01 | 2020-06-01 00:36:38.000 |        1 |  1 | 2020-06-01 00:36:38.000 |   1
2020-06-01 | 2020-06-01 00:37:38.000 |        1 |  2 | 2020-06-01 00:36:38.000 |   1
2020-06-01 | 2020-06-01 00:39:18.000 |        1 |  3 | 2020-06-01 00:36:38.000 |   1
2020-06-01 | 2020-06-01 01:16:18.000 |        1 |  4 | 2020-06-01 01:16:18.000 |   2
2020-06-01 | 2020-06-01 03:56:38.000 |        1 |  5 | 2020-06-01 03:56:38.000 |   3
2020-06-01 | 2020-06-01 05:36:38.000 |        1 |  6 | 2020-06-01 05:36:38.000 |   4
2020-06-01 | 2020-06-01 05:37:38.000 |        1 |  7 | 2020-06-01 05:36:38.000 |   4
2020-06-01 | 2020-06-01 05:39:38.000 |        1 |  8 | 2020-06-01 05:36:38.000 |   4
2020-06-01 | 2020-06-01 05:41:38.000 |        1 |  9 | 2020-06-01 05:36:38.000 |   4
2020-06-01 | 2020-06-01 07:26:38.000 |        1 | 10 | 2020-06-01 07:26:38.000 |   5
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement