Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Custom classifications of business objects
Custom grouping of business objects
Custom counting of business objects
Summary
15. Summary

Instruction

You did well! All right, it's time to wrap things up. First, let's summarize what we've covered in this part:

  1. A CASE WHEN statement checks for one or more conditions and returns a value when it finds the first matching condition. If there is no ELSE clause and no matching conditions, CASE WHEN returns NULL.
    CASE
      WHEN condition_1 THEN result_1
      WHEN condition_2 THEN result_2
      ...
      ELSE result
    END
    
  2. To add a new column, and thus a custom classification of business products, you can use CASE WHEN in the SELECT clause:
    SELECT 
      CASE
        WHEN ... THEN ...
      END AS sample_column
    FROM table;
    
  3. You can use the custom classification to create your own grouping. In PostgreSQL you cane use the CASE WHEN clause in the SELECT clause and use the alias in the GROUP BY:
    SELECT 
      CASE
        WHEN ... THEN ...
      END AS sample_column,
      COUNT(*) AS sample_count
    FROM table
      ...
    GROUP BY sample_column;
    
  4. HERE
  5. You can create a custom count of business objects using CASE WHEN inside a COUNT() or SUM() function:
    SELECT 
      COUNT(CASE
        WHEN ... THEN column_name
      END) AS count_column
    FROM table;
    
    SELECT 
      SUM(CASE
        WHEN ... THEN 1
      END) AS count_column
    FROM table;
    

How about a short quiz now?

Exercise

Click Next exercise to continue.