Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Aplica-se a:
Databricks SQL
Runtime 19 e acima
Retorna o início do balde de tempo de largura fixa em que um carimbo de tempo se encaixa, alinhado a uma origem.
Sintaxe
time_bucket(bucketSize, ts [, origin])
Argumentos
-
bucketSize: UmaINTERVALexpressão constante que especifica o tamanho de cada balde. Suporta intervalos diurnos e de um ano-mês. Deve ser positivo e diferente de zero. -
ts: A ouTIMESTAMP_NTZexpressão para bucketTIMESTAMP. -
origin: Uma expressão opcionalTIMESTAMPouTIMESTAMP_NTZconstante que define a âncora de alinhamento da grade de balde. Usa1970-01-01 00:00:00como padrão. Deve ser do mesmo tipo quets.
Returns
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.
Notas
-
time_bucketdivide o eixo de tempo em intervalos consecutivos, não sobrepostos, de tamanhobucketSize, ancorados emorigin. Ele retorna o início do intervalo que contémts. -
originnão precisa precederts. Ela define apenas o alinhamento da grade do balde. A grade se estende infinitamente em ambas as direções a partiroriginde . - Para
TIMESTAMP_NTZ,time_bucketbaldes em UTC. ParaTIMESTAMP, os segmentos de intervalo ano-mês e os componentes de dia do calendário dos baldes de intervalo diurno-horário alinham-se ao fuso horário da sessão. - Se qualquer argumento for
NULL, o resultado éNULL. - Use
date_truncpara limites alinhados ao calendário, como agrupar por mês do calendário. Usetime_bucketquando a largura ou alinhamento do balde for definido pelo usuário.
Condições de erro comuns
- DATATYPE_MISMATCH. NON_FOLDABLE_INPUT
- DATATYPE_MISMATCH. UNEXPECTED_INPUT_TYPE
- DATATYPE_MISMATCH. VALUE_OUT_OF_RANGE
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).