-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe_PADS.sql
More file actions
71 lines (66 loc) · 2.13 KB
/
Copy paththe_PADS.sql
File metadata and controls
71 lines (66 loc) · 2.13 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
/*
Task:
Generate the following two result sets:
1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses).
For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name.
If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
Note: There will be at least two entries in the table for each type of occupation.
Input Format
The OCCUPATIONS table is described as follows: Name (String), Occupation (String)
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
*/
/*
goal 1: output list with names and initial of profession
goal 2: count number of people in profession and output as a string
goal 2 requires an aggregate in a table
- needs strings in column rows for both, and is 2 queries
*/
-- goal 1
WITH
profession_sub AS (
SELECT
name,
SUBSTRING(occupation, 1, 1) AS profession_initial
FROM
occupations)
SELECT
CONCAT(o.name, '(', p.profession_initial, ')')
FROM
occupations o
JOIN
profession_sub p
ON
o.name = p.name
ORDER BY
o.name;
-- goal 2
WITH
table_occ_count AS (
SELECT
LOWER(occupation) AS lower_occupation,
COUNT(occupation) AS occupation_count
FROM
occupations
GROUP BY
occupation),
final_string AS (
SELECT
DISTINCT toc.lower_occupation AS final_occupation,
toc.occupation_count AS final_count,
CONCAT('There are a total of ', toc.occupation_count, ' ', toc.lower_occupation, 's.') AS occupation_string
FROM
table_occ_count toc
JOIN
occupations o
ON
toc.lower_occupation = LOWER(o.occupation) )
SELECT
occupation_string
FROM
final_string fs
ORDER BY
fs.final_count,
fs.final_occupation