Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Aplica-se a:
Databricks SQL
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 especificaINTERVALo tamanho de cada balde. Suporta intervalos diurnos e ano-mês. Tem de 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 grelha bucket. O valor padrão é1970-01-01 00:00:00. Deve ser do mesmo tipo quets.
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_bucketdivide o eixo do tempo em intervalos consecutivos e não sobrepostos de tamanhobucketSize, ancorados emorigin. Devolve o início do intervalo que contémts. -
originnão precisa de precederts. Define apenas o alinhamento da grelha do balde. A grelha estende-se infinitamente em ambas as direções a partir deorigin. - Para
TIMESTAMP_NTZ,time_bucketbaldes em UTC. ParaTIMESTAMP, 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_truncpara limites alinhados com o calendário, como agrupar por mês do calendário. Usetime_bucketquando a largura ou alinhamento do balde for definido pelo utilizador.
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).