session_window
espressione di raggruppamento
Si applica a: Databricks SQL Databricks Runtime 10.4 LTS e versioni successive
Crea una finestra di sessione in un'espressione data e ora.
Sintassi
session_window(expr, gapDuration)
Argomenti
expr
TIMESTAMP
: espressione che specifica l'oggetto della finestra.gapDuration
STRING
: espressione che rappresenta la larghezza della finestra come valoreINTERVAL DAY TO SECOND
letterale.
Valori restituiti
Restituisce un set di raggruppamenti su cui è possibile operare con funzioni di aggregazione.
Il nome della GROUP BY
colonna è session_window
. È di tipo STRUCT<start:TIMESTAMP, end:TIMESTAMP>
Esempi
> 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