sql - How to Count Distinct on Case When?

Sql - How to Count Distinct on Case When?

You can count distinct values based on a CASE expression in SQL by nesting the CASE expression inside the COUNT(DISTINCT ...) function. Here's how you can do it:

SELECT COUNT(DISTINCT CASE WHEN condition1 THEN column1 WHEN condition2 THEN column2 ... END) AS distinct_count FROM your_table; 

In this query:

  • Replace condition1, condition2, etc., with your specific conditions.
  • Replace column1, column2, etc., with the columns you want to count distinct values for.
  • The CASE expression evaluates each row against the specified conditions and returns the corresponding column value.
  • The COUNT(DISTINCT ...) function then counts the distinct values returned by the CASE expression.

Example:

Let's say you have a table sales with columns product_id and customer_id, and you want to count the distinct number of customers who bought a specific product or belong to a certain category of products:

SELECT COUNT(DISTINCT CASE WHEN product_id = 1 THEN customer_id WHEN product_id IN (2, 3, 4) THEN customer_id END) AS distinct_customer_count FROM sales; 

This query counts the distinct number of customers who bought product with product_id 1 or products with product_id 2, 3, or 4.

Note:

  • The CASE expression can include multiple conditions, and each condition can return a different column value.
  • The COUNT(DISTINCT ...) function calculates the distinct count based on the values returned by the CASE expression.

Examples

  1. How to count distinct values based on a conditional in SQL?

    • Description: This query counts distinct values of a column based on conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN condition1 THEN column1 END) AS distinct_count FROM your_table; 
  2. How to count distinct values for multiple conditions using CASE WHEN in SQL?

    • Description: This query counts distinct values based on multiple conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN condition1 THEN column1 WHEN condition2 THEN column2 END) AS distinct_count FROM your_table; 
  3. How to count distinct values with grouping using CASE WHEN in SQL?

    • Description: This query counts distinct values with grouping based on conditions using CASE WHEN.
    • Code:
      SELECT category_column, COUNT(DISTINCT CASE WHEN condition1 THEN column1 END) AS distinct_count FROM your_table GROUP BY category_column; 
  4. How to count distinct values for each group using CASE WHEN in SQL?

    • Description: This query counts distinct values within each group based on conditions using CASE WHEN.
    • Code:
      SELECT category_column, COUNT(DISTINCT CASE WHEN condition1 THEN column1 END) AS distinct_count FROM your_table GROUP BY category_column; 
  5. How to count distinct values with filters using CASE WHEN in SQL?

    • Description: This query counts distinct values with additional filters based on conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN condition1 AND filter_column = 'value' THEN column1 END) AS distinct_count FROM your_table; 
  6. How to count distinct values based on nested conditions using CASE WHEN in SQL?

    • Description: This query counts distinct values with nested conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN condition1 OR (condition2 AND condition3) THEN column1 END) AS distinct_count FROM your_table; 
  7. How to count distinct values using CASE WHEN with date conditions in SQL?

    • Description: This query counts distinct values based on date conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN DATEPART(year, date_column) = 2023 THEN column1 END) AS distinct_count FROM your_table; 
  8. How to count distinct values using CASE WHEN with boolean conditions in SQL?

    • Description: This query counts distinct values based on boolean conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN condition_column = 'true' THEN column1 END) AS distinct_count FROM your_table; 
  9. How to count distinct values using CASE WHEN with NULL conditions in SQL?

    • Description: This query counts distinct values based on NULL conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN nullable_column IS NULL THEN column1 END) AS distinct_count FROM your_table; 
  10. How to count distinct values using CASE WHEN with string conditions in SQL?

    • Description: This query counts distinct values based on string conditions using CASE WHEN.
    • Code:
      SELECT COUNT(DISTINCT CASE WHEN column1 LIKE 'prefix%' THEN column1 END) AS distinct_count FROM your_table; 

More Tags

evaluation subset windows-console flutter-appbar powershell operation logstash resteasy web-scraping azure-api-apps

More Programming Questions

More Animal pregnancy Calculators

More Livestock Calculators

More Chemical reactions Calculators

More Auto Calculators