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 Use FILTER to Have Multiple Counts
In PostgreSQL, the `FILTER` clause allows you to apply aggregate functions to a specific subset of data. This can be particularly useful when you want to compute multiple counts or other aggregations based on different conditions in a single query. Below, we'll explore how to perform multiple counts using the `FILTER` clause.
Understanding the FILTER Clause
The `FILTER` clause in PostgreSQL is used in conjunction with aggregate functions to filter rows before performing the aggregation. The basic syntax looks like this:
SELECT
COUNT(*) FILTER (WHERE condition) AS condition_count,
COUNT(*) FILTER (WHERE another_condition) AS another_condition_count
FROM your_table;
In the above query, `condition` and `another_condition` are the conditions used to filter the rows before performing the count. The result will return the count for each condition in separate columns.
Example: Counting Employees Based on Status
Imagine you have an "employees" table, and you want to count how many employees are active and how many are inactive. Here's how you can achieve this using the `FILTER` clause:
SELECT
COUNT(*) FILTER (WHERE status = 'active') AS active_count,
COUNT(*) FILTER (WHERE status = 'inactive') AS inactive_count
FROM employees;
This query will return two columns: `active_count` and `inactive_count`, each representing the count of employees with the respective status.
Advantages of Using FILTER
Using the `FILTER` clause allows you to perform multiple counts or other aggregations in a more compact and efficient manner than running multiple queries. This reduces the complexity of your queries and can improve performance, especially on larger datasets.
Conclusion
The `FILTER` clause in PostgreSQL is a powerful tool that simplifies queries when you need to perform multiple counts or aggregations based on different conditions. By filtering the data before applying aggregate functions, you can achieve cleaner, more efficient SQL queries.