Repository navigation
Expand file tree
/
Copy path12_Case_Statements.sql
More file actions
133 lines (121 loc) · 3.74 KB
/
Copy path12_Case_Statements.sql
File metadata and controls
133 lines (121 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ==============================================================================
SQL CASE Statement
---------------------------------------------------------------------------------
This script demonstrates various use cases of the SQL CASE statement, including
data categorization, mapping, quick form syntax, handling nulls, and conditional
aggregation.
Table of Contents:
1. USE CASE | CATEGORIZE DATA
2. USE CASE | MAPPING
3. QUICK FORM SYNTAX
4. HANDLING NULLS
5. CONDITIONAL AGGREGATION
=================================================================================
*/
-- Run this line first: every query in this script uses SalesDB
USE SalesDB;
/* ==============================================================================
1. USE CASE | CATEGORIZE DATA
============================================================================== */
/* TASK 1:
Create a report showing total sales for each category:
- High: Sales over 50
- Medium: Sales between 20 and 50
- Low: Sales 20 or less
The results are sorted from highest to lowest total sales.
*/
SELECT
Category,
SUM(Sales) AS TotalSales
FROM (
SELECT
OrderID,
Sales,
CASE
WHEN Sales > 50 THEN 'High'
WHEN Sales > 20 THEN 'Medium'
ELSE 'Low'
END AS Category
FROM Sales.Orders
) AS t
GROUP BY Category
ORDER BY TotalSales DESC;
/* ==============================================================================
2. USE CASE | MAPPING
============================================================================== */
/* TASK 2:
Retrieve customer details with abbreviated country codes
*/
SELECT
CustomerID,
FirstName,
LastName,
Country,
CASE
WHEN Country = 'Germany' THEN 'DE'
WHEN Country = 'USA' THEN 'US'
ELSE 'n/a'
END AS CountryAbbr
FROM Sales.Customers;
/* ==============================================================================
3. QUICK FORM SYNTAX
============================================================================== */
/* TASK 3:
Retrieve customer details with abbreviated country codes using quick form
*/
SELECT
CustomerID,
FirstName,
LastName,
Country,
CASE
WHEN Country = 'Germany' THEN 'DE'
WHEN Country = 'USA' THEN 'US'
ELSE 'n/a'
END AS CountryAbbr,
CASE Country
WHEN 'Germany' THEN 'DE'
WHEN 'USA' THEN 'US'
ELSE 'n/a'
END AS CountryAbbr2
FROM Sales.Customers;
/* ==============================================================================
4. HANDLING NULLS
============================================================================== */
/* TASK 4:
Calculate the average score of customers, treating NULL as 0,
and provide CustomerID and LastName details.
*/
SELECT
CustomerID,
LastName,
Score,
CASE
WHEN Score IS NULL THEN 0
ELSE Score
END AS ScoreClean,
AVG(
CASE
WHEN Score IS NULL THEN 0
ELSE Score
END
) OVER () AS AvgCustomerClean,
AVG(Score) OVER () AS AvgCustomer
FROM Sales.Customers;
/* ==============================================================================
5. CONDITIONAL AGGREGATION
============================================================================== */
/* TASK 5:
Count how many orders each customer made with sales greater than 30
*/
SELECT
CustomerID,
SUM(
CASE
WHEN Sales > 30 THEN 1
ELSE 0
END
) AS TotalOrdersHighSales,
COUNT(*) AS TotalOrders
FROM Sales.Orders
GROUP BY CustomerID;