window grouping expression

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Creates a hopping based sliding-window over a timestamp expression.

Syntax

window(expr, width [, slide [, start] ] )

Arguments

  • expr: A TIMESTAMP expression specifying the subject of the window.

  • width: A STRING literal representing the width of the window as an INTERVAL DAY TO SECOND literal.

  • slide: An optional STRING literal representing an offset from midnight to start, expressed as an INTERVAL HOUR TO SECOND literal.

  • start: An optional STRING literal representing the start of the next window expressed as an INTERVAL DAY TO SECOND literal.

Returns

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

slide must be less than or equal to width. start must be less than slide.

If slide < width the rows in each groups overlap. By default slide equals width so expr are partitioned into groups. The windowing starts at 1970-01-01 00:00:00 UTC + start. The default for start is '0 SECONDS'

Examples

> SELECT window, min(val), max(val), count(val)
  FROM VALUES (TIMESTAMP'2020-08-01 12:20:21', 17),
              (TIMESTAMP'2020-08-01 12:20:22', 12),
              (TIMESTAMP'2020-08-01 12:23:10',  8),
              (TIMESTAMP'2020-08-01 12:25:05', 11),
              (TIMESTAMP'2020-08-01 12:28:59', 15),
              (TIMESTAMP'2020-08-01 12:30:01', 23),
              (TIMESTAMP'2020-08-01 12:30:15',  2),
              (TIMESTAMP'2020-08-01 12:35:22', 16) AS S(stamp, val)
  GROUP BY window(stamp, '2 MINUTES 30 SECONDS', '30 SECONDS', '15 SECONDS');
  {2020-08-01 12:19:15, 2020-08-01 12:21:45} 12       17       2
  {2020-08-01 12:18:15, 2020-08-01 12:20:45} 12       17       2
  {2020-08-01 12:20:15, 2020-08-01 12:22:45} 12       17       2
  {2020-08-01 12:19:45, 2020-08-01 12:22:15} 12       17       2
  {2020-08-01 12:18:45, 2020-08-01 12:21:15} 12       17       2
  {2020-08-01 12:21:45, 2020-08-01 12:24:15} 8        8        1
  {2020-08-01 12:22:45, 2020-08-01 12:25:15} 8        11       2
  {2020-08-01 12:21:15, 2020-08-01 12:23:45} 8        8        1
  {2020-08-01 12:22:15, 2020-08-01 12:24:45} 8        8        1
  {2020-08-01 12:20:45, 2020-08-01 12:23:15} 8        8        1
  {2020-08-01 12:23:45, 2020-08-01 12:26:15} 11       11       1
  {2020-08-01 12:23:15, 2020-08-01 12:25:45} 11       11       1
  {2020-08-01 12:24:45, 2020-08-01 12:27:15} 11       11       1
  {2020-08-01 12:24:15, 2020-08-01 12:26:45} 11       11       1
  {2020-08-01 12:27:15, 2020-08-01 12:29:45} 15       15       1
  {2020-08-01 12:27:45, 2020-08-01 12:30:15} 15       23       2
  {2020-08-01 12:28:45, 2020-08-01 12:31:15} 2        23       3
  {2020-08-01 12:26:45, 2020-08-01 12:29:15} 15       15       1
  {2020-08-01 12:28:15, 2020-08-01 12:30:45} 2        23       3
  {2020-08-01 12:29:45, 2020-08-01 12:32:15} 2        23       2