Jaa


session_window grouping expression

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 10.4 LTS and above

Creates a session-window over a timestamp expression.

Syntax

session_window(expr, gapDuration)

Arguments

  • expr: A TIMESTAMP expression specifying the subject of the window.
  • gapDuration: A STRING expression representing the width of the window as an INTERVAL DAY TO SECOND literal.

Returns

Returns a set of groupings that can be operated on with aggregate functions. The GROUP BY column name is session_window. It is of type STRUCT<start:TIMESTAMP, end:TIMESTAMP>

Examples

> SELECT a, session_window.start, session_window.end, count(*) as cnt
    FROM VALUES ('A1', '2021-01-01 00:00:00'),
                ('A1', '2021-01-01 00:04:30'),
                ('A1', '2021-01-01 00:10:00'),
                ('A2', '2021-01-01 00:01:00') AS tab(a, b)
    GROUP by a, session_window(b, '5 minutes')
    ORDER BY a, start;
  A1  2021-01-01 00:00:00  2021-01-01 00:09:30  2
  A1  2021-01-01 00:10:00  2021-01-01 00:15:00  1
  A2  2021-01-01 00:01:00  2021-01-01 00:06:00  1

> SELECT a, session_window.start, session_window.end, count(*) as cnt
    FROM VALUES ('A1', '2021-01-01 00:00:00'),
                ('A1', '2021-01-01 00:04:30'),
                ('A1', '2021-01-01 00:10:00'),
                ('A2', '2021-01-01 00:01:00'),
                ('A2', '2021-01-01 00:04:30') AS tab(a, b)
    GROUP by a, session_window(b, CASE WHEN a = 'A1' THEN '5 minutes'
                                       WHEN a = 'A2' THEN '1 minute'
                                       ELSE '10 minutes' END)
    ORDER BY a, start;
  A1  2021-01-01 00:00:00  2021-01-01 00:09:30  2
  A1  2021-01-01 00:10:00  2021-01-01 00:15:00  1
  A2  2021-01-01 00:01:00  2021-01-01 00:02:00  1
  A2  2021-01-01 00:04:30  2021-01-01 00:05:30  1