Truncate date and time values to a specified precision
The Postgres date_trunc() function truncates a timestamp or interval to a specified precision.
This function is particularly useful for grouping time-series data and performing time-based calculations. For example, it can be used to generate monthly reports, analyze hourly trends, or group events by time period.
Function signature
The date_trunc() function has the following form:
field: A string literal specifying the precision to which to truncate the input value. Valid values include microseconds, milliseconds, second, minute, hour, day, week, month, quarter, year, decade, century, and millennium.
source: The timestamp or interval value to be truncated.
time_zone (optional): The timezone in which to perform the truncation. Otherwise, the default timezone is used.
The function returns a timestamp or interval value truncated to the specified precision, i.e., fields less significant than the specified precision are set to zero.
Example usage
Let's consider a table called sales that tracks daily sales data. We can use date_trunc to group sales by different time periods.
This query groups sales by month, summing the total sales for each month.
We can further refine the output by extracting the month and year from the truncated timestamp:
This query groups sales by year and month, providing a more readable output:
Advanced examples
Use date_trunc with different precisions
We can use date_trunc with different precision levels to analyze data at each granularity:
This query demonstrates how date_trunc works with different precision levels, from year down to millisecond.
Use date_trunc with timezones
The date_trunc function can be used with specific timezones:
This query shows how date_trunc behaves differently when truncating to the day in different timezones.
Use date_trunc for time-based analysis
Below, we use date_trunc to analyze user activity patterns for a hypothetical social media application:
This query uses date_trunc to group user activities by each day.
Use date_trunc with interval types
The date_trunc function can also be used with interval data:
This query truncates the first interval to the nearest hour, while the second column truncates the difference between two timestamps to the nearest day.
Additional considerations
Timezone awareness
When using date_trunc with timestamps, the function uses the default timezone of the session, or that specified in the input. As shown in the previous section, the truncation result can vary depending on the timezone.
Truncating intervals
When truncating intervals, the date_trunc function rounds the interval to the nearest value based on the specified precision. However, note that the output might not be intuitive and depends on how the interval is defined.
For example, the query below attempts to truncate a month from an interval specified as some number of days.
This query outputs the following:
The first input interval didn't have a month component, so even with the number of days being bigger than a month, the output is zero. The second input interval has a month component, so the output is the input interval truncated to the month.
Performance considerations
When using date_trunc in WHERE clauses or for grouping large datasets, consider creating an index on the truncated values to improve query performance:
This creates an index on the monthly truncated sale dates, which can speed up queries that group or filter by month.