-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL49.sql
More file actions
79 lines (71 loc) · 2.32 KB
/
DataExpert-SQL49.sql
File metadata and controls
79 lines (71 loc) · 2.32 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
-- Question: Summarizing Leisure Activity Resorts by Country
-- https://www.dataexpert.io/question/leisure-activity-resorts-by-country
-- Approach 01: Using Conditional Aggregation
SELECT
country,
SUM(CASE WHEN leisure_activity_type = 'Adventure park' THEN number_of_places ELSE 0 END) AS adventure_park,
SUM(CASE WHEN leisure_activity_type = 'Golf' THEN number_of_places ELSE 0 END) AS golf,
SUM(CASE WHEN leisure_activity_type = 'River cruise' THEN number_of_places ELSE 0 END) AS river_cruise,
SUM(CASE WHEN leisure_activity_type = 'Kart racing' THEN number_of_places ELSE 0 END) AS kart_racing
FROM
playground.country_activities
GROUP BY
country
ORDER BY
country;
-- Approach 02: Using PIVOT with Subquery to Exclude Unwanted Columns
SELECT
country,
COALESCE("'River cruise'", 0) AS "River cruise",
COALESCE("'Golf'", 0) AS "Golf",
COALESCE("'Adventure park'", 0) AS "Adventure park",
COALESCE("'Kart racing'", 0) AS "Kart racing"
FROM (
-- This subquery is the key.
-- We select ONLY the columns needed for the pivot.
-- This hides the 'region' column from the PIVOT operator.
SELECT
country,
leisure_activity_type,
number_of_places
FROM
playground.country_activities
)
PIVOT (
-- The PIVOT runs on the subquery.
-- Since 'region' is gone, it correctly groups by 'country'.
SUM(number_of_places)
FOR leisure_activity_type IN (
'River cruise', 'Golf', 'Adventure park', 'Kart racing'
)
)
ORDER BY
country;
-- Approach 02: MS SQL Server Version
SELECT
country,
COALESCE("'River cruise'", 0) AS "River cruise",
COALESCE("'Golf'", 0) AS "Golf",
COALESCE("'Adventure park'", 0) AS "Adventure park",
COALESCE("'Kart racing'", 0) AS "Kart racing"
FROM (
-- This subquery is the key.
-- We select ONLY the columns needed for the pivot.
-- This hides the 'region' column from the PIVOT operator.
SELECT
country,
leisure_activity_type,
number_of_places
FROM
playground.country_activities
) AS SourceTable
PIVOT (
-- The PIVOT runs on the subquery.
-- Since 'region' is gone, it correctly groups by 'country'.
SUM(number_of_places)
FOR leisure_activity_type IN (
[River cruise], [Golf], [Adventure park], [Kart racing]
)
) AS PivotTable
ORDER BY
country;