Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre CASE WHEN for Custom Reports | CASE WHEN for Aggregates and Reporting
Mastering CASE WHEN in SQL

bookCASE WHEN for Custom Reports

Business reporting often requires you to deliver insights tailored to specific needs, such as segmenting customers, measuring different transaction types, or identifying key groups like VIPs. Standard SQL aggregates can summarize totals, but real-world scenarios demand more flexibility. By using CASE WHEN within your queries, you can define custom metrics and categories on the fly, making your reports more meaningful and actionable for business stakeholders. This approach allows you to create new columns that break down data by custom rules instead of only relying on existing fields.

123456789
SELECT customer_id, SUM(CASE WHEN amount >= 200 THEN amount ELSE 0 END) AS Large_Transactions, SUM(CASE WHEN amount >= 100 AND amount < 200 THEN amount ELSE 0 END) AS Medium_Transactions, SUM(CASE WHEN amount < 100 THEN amount ELSE 0 END) AS Small_Transactions FROM transactions GROUP BY customer_id;
copy

In this query, you see how to structure CASE WHEN expressions inside aggregate functions to create multiple custom columns in a single report. Each column uses a different CASE WHEN condition to sum transaction amounts that match certain thresholds: Large_Transactions sums amounts of $200 or more, Medium_Transactions covers amounts between $100 and $199.99, and Small_Transactions includes amounts less than $100. By grouping the results by customer_id, you provide a customer-level breakdown that is easy to interpret and directly supports business decisions. This pattern can be adapted for any custom segmentation needed in your reporting.

123456789101112
SELECT customer_id, COUNT(*) AS total_transactions, SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) AS large_transaction_count, CASE WHEN SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) > 5 THEN 'VIP' ELSE 'Regular' END AS customer_status FROM transactions GROUP BY customer_id;
copy

1. How can CASE WHEN help in creating custom business reports?

2. Fill in the blanks to create a CASE WHEN that flags customers with more than 5 'Large' transactions (amount >= 200) as 'VIP':

question mark

How can CASE WHEN help in creating custom business reports?

Select all correct answers

question-icon

Fill in the blanks to create a CASE WHEN that flags customers with more than 5 'Large' transactions (amount >= 200) as 'VIP':

SELECT customer_id, CASE WHEN SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) > THEN '' ELSE 'Regular' END AS customer_status FROM transactions GROUP BY customer_id;
customer_id | customer_status
-------------+-----------------
101 | Regular
102 | Regular
103 | Regular
104 | Regular

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookCASE WHEN for Custom Reports

Glissez pour afficher le menu

Business reporting often requires you to deliver insights tailored to specific needs, such as segmenting customers, measuring different transaction types, or identifying key groups like VIPs. Standard SQL aggregates can summarize totals, but real-world scenarios demand more flexibility. By using CASE WHEN within your queries, you can define custom metrics and categories on the fly, making your reports more meaningful and actionable for business stakeholders. This approach allows you to create new columns that break down data by custom rules instead of only relying on existing fields.

123456789
SELECT customer_id, SUM(CASE WHEN amount >= 200 THEN amount ELSE 0 END) AS Large_Transactions, SUM(CASE WHEN amount >= 100 AND amount < 200 THEN amount ELSE 0 END) AS Medium_Transactions, SUM(CASE WHEN amount < 100 THEN amount ELSE 0 END) AS Small_Transactions FROM transactions GROUP BY customer_id;
copy

In this query, you see how to structure CASE WHEN expressions inside aggregate functions to create multiple custom columns in a single report. Each column uses a different CASE WHEN condition to sum transaction amounts that match certain thresholds: Large_Transactions sums amounts of $200 or more, Medium_Transactions covers amounts between $100 and $199.99, and Small_Transactions includes amounts less than $100. By grouping the results by customer_id, you provide a customer-level breakdown that is easy to interpret and directly supports business decisions. This pattern can be adapted for any custom segmentation needed in your reporting.

123456789101112
SELECT customer_id, COUNT(*) AS total_transactions, SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) AS large_transaction_count, CASE WHEN SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) > 5 THEN 'VIP' ELSE 'Regular' END AS customer_status FROM transactions GROUP BY customer_id;
copy

1. How can CASE WHEN help in creating custom business reports?

2. Fill in the blanks to create a CASE WHEN that flags customers with more than 5 'Large' transactions (amount >= 200) as 'VIP':

question mark

How can CASE WHEN help in creating custom business reports?

Select all correct answers

question-icon

Fill in the blanks to create a CASE WHEN that flags customers with more than 5 'Large' transactions (amount >= 200) as 'VIP':

SELECT customer_id, CASE WHEN SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) > THEN '' ELSE 'Regular' END AS customer_status FROM transactions GROUP BY customer_id;
customer_id | customer_status
-------------+-----------------
101 | Regular
102 | Regular
103 | Regular
104 | Regular

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 4
some-alt