Funzione time_bucket

Si applica a:spunta sì Databricks SQL spuntato sì Databricks Runtime 19 e superiori

Restituisce l'inizio del bucket temporale a larghezza fissa in cui si inserisce un timestamp, allineato a un'origine.

Sintassi

time_bucket(bucketSize, ts [, origin])

Argomenti

  • bucketSize: Un'espressione costante che specifica la dimensione INTERVAL di ogni bucket. Supporta intervalli diurni e annuali-mensili. Deve essere positivo e diverso da zero.
  • ts: A o TIMESTAMP_NTZ espressione a secchioTIMESTAMP.
  • origin: Un'espressione opzionale TIMESTAMP o TIMESTAMP_NTZ costante che definisce l'ancora di allineamento della griglia del secchio. Di default è 1970-01-01 00:00:00. Deve essere dello stesso tipo di ts.

Returns

Un valore dello stesso tipo di ts (TIMESTAMP o TIMESTAMP_NTZ). Il valore è l'inizio del secchio [start, start + bucketSize) semi-aperto che contiene ts. L'inizio è sempre minore o uguale a ts, e il confine del secchio successivo (start + bucketSize) è strettamente maggiore di ts.

Note

  • time_bucket divide l'asse temporale in intervalli consecutivi e non sovrapposti di dimensione bucketSize, ancorati a origin. Restituisce l'inizio dell'intervallo che contiene ts.
  • origin non deve necessariamente precedere ts. Definisce solo l'allineamento della griglia del secchio. La griglia si estende all'infinito in entrambe le direzioni da origin.
  • Per TIMESTAMP_NTZ, time_bucket bucket in UTC. Per TIMESTAMP, i bucket annual-month e le componenti calendario day dei bucket daytime si allineano al fuso orario della sessione.
  • Se un qualsiasi argomento è NULL, il risultato è NULL.
  • Usa date_trunc per confini allineati al calendario, come raggruppare per mese del calendario. Da usare time_bucket quando la larghezza o l'allineamento del secchio è definito dall'utente.

Condizioni di errore comuni

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