-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1990-CountTheNumberOfExperiments.sql
More file actions
95 lines (90 loc) · 3.93 KB
/
1990-CountTheNumberOfExperiments.sql
File metadata and controls
95 lines (90 loc) · 3.93 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
-- 1990. Count the Number of Experiments
-- Table: Experiments
-- +-----------------+------+
-- | Column Name | Type |
-- +-----------------+------+
-- | experiment_id | int |
-- | platform | enum |
-- | experiment_name | enum |
-- +-----------------+------+
-- experiment_id is the column with unique values for this table.
-- platform is an enum (category) type of values ('Android', 'IOS', 'Web').
-- experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').
-- This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.
-- Write a solution to report the number of experiments done on each of the three platforms for each of the three given experiments. Notice that all the pairs of (platform, experiment) should be included in the output including the pairs with zero experiments.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Experiments table:
-- +---------------+----------+-----------------+
-- | experiment_id | platform | experiment_name |
-- +---------------+----------+-----------------+
-- | 4 | IOS | Programming |
-- | 13 | IOS | Sports |
-- | 14 | Android | Reading |
-- | 8 | Web | Reading |
-- | 12 | Web | Reading |
-- | 18 | Web | Programming |
-- +---------------+----------+-----------------+
-- Output:
-- +----------+-----------------+-----------------+
-- | platform | experiment_name | num_experiments |
-- +----------+-----------------+-----------------+
-- | Android | Reading | 1 |
-- | Android | Sports | 0 |
-- | Android | Programming | 0 |
-- | IOS | Reading | 0 |
-- | IOS | Sports | 1 |
-- | IOS | Programming | 1 |
-- | Web | Reading | 2 |
-- | Web | Sports | 0 |
-- | Web | Programming | 1 |
-- +----------+-----------------+-----------------+
-- Explanation:
-- On the platform "Android", we had only one "Reading" experiment.
-- On the platform "IOS", we had one "Sports" experiment and one "Programming" experiment.
-- On the platform "Web", we had two "Reading" experiments and one "Programming" experiment.
-- Create table If Not Exists Experiments (experiment_id int, platform ENUM('Android', 'IOS', 'Web'), experiment_name ENUM('Reading', 'Sports', 'Programming'))
-- Truncate table Experiments
-- insert into Experiments (experiment_id, platform, experiment_name) values ('4', 'IOS', 'Programming')
-- insert into Experiments (experiment_id, platform, experiment_name) values ('13', 'IOS', 'Sports')
-- insert into Experiments (experiment_id, platform, experiment_name) values ('14', 'Android', 'Reading')
-- insert into Experiments (experiment_id, platform, experiment_name) values ('8', 'Web', 'Reading')
-- insert into Experiments (experiment_id, platform, experiment_name) values ('12', 'Web', 'Reading')
-- insert into Experiments (experiment_id, platform, experiment_name) values ('18', 'Web', 'Programming')
WITH
p AS (
SELECT "IOS" AS platform
UNION ALL
SELECT "Android" AS platform
UNION ALL
SELECT "Web" AS platform
),
e AS (
SELECT "Reading" AS experiment_name
UNION ALL
SELECT "Sports" AS experiment_name
UNION ALL
SELECT "Programming" AS experiment_name
)
SELECT
i.*,
COUNT(DISTINCT experiment_id ) AS num_experiments
FROM
(
SELECT
p.platform,
e.experiment_name
FROM
p, e
) AS i
LEFT JOIN
Experiments AS ex
ON
i.platform = ex.platform AND
i.experiment_name = ex.experiment_name
GROUP BY
i.platform, i.experiment_name
ORDER BY
i.platform, i.experiment_name