time_bucket Função

Aplica-se a:assinalado Databricks SQL assinalado sim Databricks Runtime 19 e superiores

Devolve o início do balde de tempo de largura fixa onde se insere um carimbo temporal, alinhado com uma origem.

Syntax

time_bucket(bucketSize, ts [, origin])

Arguments

  • bucketSize: Uma expressão constante que especifica INTERVAL o tamanho de cada balde. Suporta intervalos diurnos e ano-mês. Tem de ser positivo e diferente de zero.
  • ts: A ou TIMESTAMP_NTZ expressão para bucketTIMESTAMP.
  • origin: Uma expressão opcional TIMESTAMP ou TIMESTAMP_NTZ constante que define a âncora de alinhamento da grelha bucket. O valor padrão é 1970-01-01 00:00:00. Deve ser do mesmo tipo que ts.

Devoluções

Um valor do mesmo tipo que ts (TIMESTAMP ou TIMESTAMP_NTZ). O valor é o início do balde [start, start + bucketSize) semiaberto que contém ts. O início é sempre menor ou igual a ts, e a próxima fronteira do balde (start + bucketSize) é estritamente maior que ts.

Notes

  • time_bucket divide o eixo do tempo em intervalos consecutivos e não sobrepostos de tamanho bucketSize, ancorados em origin. Devolve o início do intervalo que contém ts.
  • origin não precisa de preceder ts. Define apenas o alinhamento da grelha do balde. A grelha estende-se infinitamente em ambas as direções a partir de origin.
  • Para TIMESTAMP_NTZ, time_bucket baldes em UTC. Para TIMESTAMP, os segmentos de intervalo ano-mês e os componentes do dia do calendário dos intervalos diurnos alinham-se com o fuso horário da sessão.
  • Se qualquer argumento for NULL, o resultado é NULL.
  • Use date_trunc para limites alinhados com o calendário, como agrupar por mês do calendário. Use time_bucket quando a largura ou alinhamento do balde for definido pelo utilizador.

Condições de erro comuns

Exemplos

-- 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).