session_window
grouping expression
Applies to: Databricks SQL Databricks Runtime 10.4 LTS and above
Creates a session-window over a timestamp expression.
Syntax
session_window(expr, gapDuration)
Arguments
expr
: ATIMESTAMP
expression specifying the subject of the window.gapDuration
: ASTRING
expression representing the width of the window as anINTERVAL 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