time_bucket Función

Aplica a:con marca de verificación sí Databricks SQL marcado sí Databricks Runtime 19 y superiores

Devuelve el inicio del cubo de tiempo de ancho fijo en el que se encuentra una marca de tiempo, alineado con un origen.

Sintaxis

time_bucket(bucketSize, ts [, origin])

Argumentos

  • bucketSize: Una expresión constante que especifica INTERVAL el tamaño de cada cubo. Soporta intervalos diurnos y anual-mes. Debe ser positivo y distinto de cero.
  • ts: A o TIMESTAMP_NTZ expresión a bucketTIMESTAMP.
  • origin: Una expresión opcional TIMESTAMP o TIMESTAMP_NTZ constante que define el ancla de alineación de la cuadrícula de cubo. Tiene como valor predeterminado 1970-01-01 00:00:00. Debe ser del mismo tipo que ts.

Returns

Un valor del mismo tipo que ts (TIMESTAMP o TIMESTAMP_NTZ). El valor es el inicio del cubo [start, start + bucketSize) medio abierto que contiene ts. El inicio es siempre menor o igual a ts, y el siguiente límite de cubo (start + bucketSize) es estrictamente mayor que ts.

Notas

  • time_bucket divide el eje temporal en intervalos consecutivos y no solapados de tamaño bucketSize, anclados en origin. Devuelve el inicio del intervalo que contiene ts.
  • origin no necesita preceder tsa . Solo define la alineación de la cuadrícula del cubo. La malla se extiende infinitamente en ambas direcciones desde origin.
  • Para TIMESTAMP_NTZ, time_bucket cubos en UTC. Para TIMESTAMP, los cubos de intervalo año-mes y los componentes de día del calendario de los cubos de intervalo día-tiempo se alinean con la zona horaria de la sesión.
  • Si cualquier argumento es NULL, el resultado es NULL.
  • Úsalo date_trunc para límites alineados con el calendario, como agrupar por mes calendario. Úsalo time_bucket cuando el ancho o alineación del cubo está definido por el usuario.

Condiciones de error comunes

Examples

-- 15-minute buckets (default origin at epoch)
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP '2024-01-01 11:27:00');
 2024-01-01 11:15:00

-- 1-hour buckets
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00');
 2024-01-01 11:00:00

-- Custom origin shifts bucket alignment to :05 past the hour
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00', TIMESTAMP '1970-01-01 00:05:00');
 2024-01-01 11:05:00

-- TIMESTAMP_NTZ variant
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP_NTZ '2024-01-01 11:27:00');
 2024-01-01 11:15:00

-- ts exactly on a bucket boundary returns itself
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP '2024-01-01 11:15:00');
 2024-01-01 11:15:00

-- Origin after ts: grid extends backward
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00', TIMESTAMP '2025-01-01 00:30:00');
 2024-01-01 10:30:00

-- Monthly buckets (default epoch origin: 1st of month)
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-15 11:27:00');
 2024-03-01 00:00:00

-- Quarterly buckets
> SELECT time_bucket(INTERVAL '3' MONTH, TIMESTAMP '2024-05-15 10:00:00');
 2024-04-01 00:00:00

-- Monthly buckets with origin on the 15th
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-20 09:00:00', TIMESTAMP '1970-01-15 00:00:00');
 2024-03-15 00:00:00

-- Origin on 31st: day clamping in short months
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-01 12:00:00', TIMESTAMP '1970-01-31 00:00:00');
 2024-02-29 00:00:00

-- Zero bucket width is rejected
> SELECT time_bucket(INTERVAL '0' SECOND, TIMESTAMP '2024-01-01 11:00:00');
 [DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE] The bucketSize must be between (0, inf) (current value = INTERVAL '00' SECOND).