Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Exclude Current or Partial Weeks in PostgreSQL
When working with time-series data, you may want to exclude the current or partial weeks to focus on complete weeks in your analysis. PostgreSQL provides a robust set of date and time functions that can help you filter out data from the current or partial weeks.
Excluding the Current Week
To exclude the current week, we can compare the week of the current date with the week number of the data. This is useful for reports where the current week might not yet be complete. Here’s an example:
SELECT *
FROM your_table
WHERE EXTRACT(week FROM your_date_column) < EXTRACT(week FROM CURRENT_DATE);
This query compares the week number of your data (`your_date_column`) with the current date's week number. The `EXTRACT(week FROM ...)` function pulls out the week number from a date or timestamp. We use `CURRENT_DATE` to get today’s date and filter out rows from the current week.
Excluding Partial Weeks
Sometimes, you may want to exclude partial weeks, meaning any data that falls within the current week but before the end of the week. This is useful if you want to consider only complete weeks. Here’s an example of excluding partial weeks:
SELECT *
FROM your_table
WHERE your_date_column < date_trunc('week', CURRENT_DATE);
In this case, `date_trunc('week', CURRENT_DATE)` truncates the current date to the beginning of the week (Monday). By excluding all rows with a `your_date_column` earlier than the start of the current week, we ensure that we only get data for complete weeks.
Working with Time Zones
When your data spans multiple time zones, you might need to take that into account when excluding weeks. PostgreSQL allows you to work with time zones using the `AT TIME ZONE` function. For instance:
SELECT *
FROM your_table
WHERE your_date_column AT TIME ZONE 'UTC' < date_trunc('week', CURRENT_DATE AT TIME ZONE 'UTC');
This ensures that all the dates are adjusted to the same time zone before performing the week truncation.
Conclusion
Excluding current or partial weeks is straightforward in PostgreSQL. By using date functions like `EXTRACT(week FROM ...)` and `date_trunc('week', ...)`, you can tailor your queries to exclude data that doesn’t fall into complete weeks. This method is especially helpful when you want to perform weekly reporting or analysis, ensuring that only full weeks are considered.