View a markdown version of this page

Truncation and bucketing functions - Amazon Athena

Truncation and bucketing functions

Use these functions to round a date or time value down to the start of a period, such as a month, an hour, or a 15-minute bucket. Use the result in GROUP BY to get a total for each period. Another function returns the last day of a date's month.

date_trunc function

Returns value with every part smaller than part set to its lowest value. For example, 'month' returns midnight on the first day of the month, and 'hour' sets the minutes, seconds, and fractions of a second to zero. With 'week', the result is the Monday of the week. If value is NULL, the result is NULL.

For a DATE, the result is a TIMESTAMP, not a DATE, so cast the result when you need a date. For an INTERVAL, the parts smaller than part are removed. datetrunc takes the same arguments and returns the same results.

Syntax

date_trunc(part, value)

Arguments

The date_trunc function takes the following arguments:

part

The name of the unit to truncate to, as a VARCHAR value, such as 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', or 'second'. Case is ignored. For the full list of part names, see Date and time functions and operators in DuckDB.

value

The value to truncate, as a DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or INTERVAL value. TIME values are not supported.

Return type

TIMESTAMP for a DATE or a TIMESTAMP, TIMESTAMP WITH TIME ZONE for a TIMESTAMP WITH TIME ZONE, and INTERVAL for an INTERVAL.

Examples

The following SQL query truncates a TIMESTAMP WITH TIME ZONE value to the hour. The result is shown in UTC, with the offset +00.

SELECT date_trunc( 'hour', TIMESTAMPTZ '1992-09-20 20:38:40' ) AS result

The query returns the following result.

result

1992-09-20 20:00:00+00

The following SQL query truncates a date to the month. The result is a TIMESTAMP, and the second column casts it back to DATE.

SELECT date_trunc('month', DATE '2026-11-18') AS truncated, CAST( date_trunc('month', DATE '2026-11-18') AS DATE ) AS as_date

The query returns the following result.

truncated as_date

2026-11-01 00:00:00

2026-11-01

The following SQL query truncates a Sunday, November 22, 2026, to the week. Weeks start on Monday, so the result is November 16.

SELECT date_trunc('week', DATE '2026-11-22') AS result

The query returns the following result.

result

2026-11-16 00:00:00

The following SQL query truncates an interval to the hour.

SELECT date_trunc( 'hour', INTERVAL '1 day 04:05:06' ) AS result

The query returns the following result.

result

1 day 04:00:00

Considerations

A TIMESTAMP WITH TIME ZONE value is truncated in UTC. For example, date_trunc('day', TIMESTAMPTZ '2026-11-18 22:00:00-05') returns 2026-11-19 00:00:00+00, because 22:00 at UTC-5 is 03:00 on November 19 in UTC.

'decade', 'century', and 'millennium' truncate to a year that ends in 0, 00, or 000. For example, date_trunc('century', DATE '2026-11-18') returns 2000-01-01 00:00:00, although the century function counts the 21st century from 2001.

Differences from Athena engine version 3

In Athena engine version 3, date_trunc of a DATE returns a DATE. In DuckDB, it returns a TIMESTAMP. To get a DATE, use CAST(date_trunc(unit, d) AS DATE).

last_day function

Returns the DATE of the final day in the month that contains ts. The result accounts for month lengths and leap years, so February 2024 ends on the 29th. The time of day in a timestamp is dropped.

A TIMESTAMP WITH TIME ZONE value is read in the session time zone, which is UTC. As a result, the month can differ from the month in the time zone of the original value. If ts is NULL, the result is NULL.

Syntax

last_day(ts)

Arguments

The last_day function takes the following argument:

ts

A value in the month to find the last day of, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

DATE, for every input type.

Examples

The following SQL query returns the last day of the month of a timestamp. The result is a DATE.

SELECT last_day( TIMESTAMP '1992-03-22 01:02:03.1234' ) AS result

The query returns the following result.

result

1992-03-31

The following SQL query returns the last day of February in a leap year and in a common year.

SELECT last_day(DATE '2024-02-10') AS leap_year, last_day(DATE '2023-02-10') AS common_year

The query returns the following result.

leap_year common_year

2024-02-29

2023-02-28

Differences from Athena engine version 3

Athena engine version 3 names this function last_day_of_month. DuckDB has no function with that name, so replace last_day_of_month(x) with last_day(x).

time_bucket function

Divides time into consecutive buckets that are each bucket_width long, and returns the start of the bucket that contains value. Use it to group rows by intervals such as 15 minutes, 1 day, or 1 week. If value is NULL, the result is NULL.

By default, buckets of days, hours, minutes, or seconds are counted from Monday, January 3, 2000, at midnight, so weekly buckets start on Mondays. Buckets of months or years are counted from January 1, 2000, so 3-month buckets match calendar quarters. To move the boundaries, pass an origin that starts a bucket, or an offset that shifts every boundary. For a TIMESTAMP WITH TIME ZONE value, you can instead pass a timezone, and the buckets follow the local time in that zone.

Syntax

time_bucket(bucket_width, value [, origin ]) time_bucket(bucket_width, value, offset) time_bucket(bucket_width, value, timezone)

Arguments

The time_bucket function takes the following arguments:

bucket_width

The length of each bucket, as an INTERVAL, such as INTERVAL '15 minutes'. It must be greater than zero. It can be a number of months and years, or a number of days and smaller units, but not both.

value

The value to place in a bucket, as a DATE, TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

origin

The start of a bucket, as a value of the same type as value. Every bucket boundary is a whole number of bucket_width intervals from it. This argument is optional.

offset

The amount to shift every bucket boundary from the default origin, as an INTERVAL. For example, with daily buckets, INTERVAL '6 hours' starts each day at 06:00. This argument is optional.

timezone

The time zone whose local time the buckets follow, as a VARCHAR time zone name, such as 'America/New_York'. The default is UTC. It applies to TIMESTAMP WITH TIME ZONE values only. This argument is optional.

Return type

The same type as value: DATE, TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE.

Examples

The following SQL query returns the start of the 15-minute bucket that contains a timestamp.

SELECT time_bucket( INTERVAL '15 minutes', TIMESTAMP '2024-05-01 10:37:12' ) AS result

The query returns the following result.

result

2024-05-01 10:30:00

The following SQL query counts events in 15-minute buckets. A timestamp exactly on a boundary, such as 10:15:00, starts a new bucket.

SELECT time_bucket( INTERVAL '15 minutes', ts ) AS bucket_start, count(*) AS events FROM (VALUES (TIMESTAMP '2024-05-01 10:02:00'), (TIMESTAMP '2024-05-01 10:14:59'), (TIMESTAMP '2024-05-01 10:15:00'), (TIMESTAMP '2024-05-01 10:44:30') ) AS v(ts) GROUP BY bucket_start ORDER BY bucket_start

The query returns the following results.

bucket_start events

2024-05-01 10:00:00

2

2024-05-01 10:15:00

1

2024-05-01 10:30:00

1

The following SQL query returns the start of the week that contains a date. With the default origin, weeks start on Monday, and a DATE input returns a DATE.

SELECT time_bucket( INTERVAL '1 week', DATE '2024-05-01' ) AS week_start, dayname( time_bucket( INTERVAL '1 week', DATE '2024-05-01' ) ) AS week_start_day

The query returns the following result.

week_start week_start_day

2024-04-29

Monday

The following SQL query uses daily buckets in the America/New_York time zone. The input time, 03:00 on May 1 in UTC, is 23:00 on April 30 in New York. The function returns the start of that New York day, April 30, which is 04:00 in UTC.

SELECT time_bucket( INTERVAL '1 day', TIMESTAMPTZ '2024-05-01 03:00:00+00', 'America/New_York' ) AS result

The query returns the following result.

result

2024-04-30 04:00:00+00

Considerations

A bucket_width of zero or less makes the query fail. So does an interval that mixes months with days or smaller units, such as INTERVAL '1 month 1 day'.

To start weekly buckets on Sunday, pass a Sunday as origin, for example time_bucket(INTERVAL '1 week', value, DATE '2000-01-02').

A DATE is treated as midnight at the start of that day. When the bucket that contains that midnight starts on an earlier day, the function returns that earlier day. This can happen with an offset, such as INTERVAL '6 hours' with daily buckets. It can also happen with a bucket_width that is not a whole number of days, such as INTERVAL '5 hours'. If you pass a TIMESTAMP as origin, the result is a TIMESTAMP instead of a DATE.