-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL19.sql
More file actions
39 lines (31 loc) · 967 Bytes
/
DataExpert-SQL19.sql
File metadata and controls
39 lines (31 loc) · 967 Bytes
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
-- Question: Who are the top 10 authors by number of reviews?
-- https://www.dataexpert.io/question/top-10-authors-by-reviews
-- Answer from DataExpert.io:
-- https://www.linkedin.com/posts/eczachly_dataengineering-activity-7164347913316290560-hTF3/
-- Approach 1:
SELECT author,
SUM(CAST(REPLACE(no_of_reviews, ',', '') AS BIGINT)) AS number_of_reviews
FROM bootcamp.books
WHERE TRIM(no_of_reviews) <> ''
GROUP BY author
ORDER BY number_of_reviews DESC
--LIMIT 10;
-- Approach 2:
SELECT author,
SUM(CAST(REGEXP_REPLACE(no_of_reviews, '[^0-9]', '') AS BIGINT)) AS number_of_reviews
FROM bootcamp.books
GROUP BY author
ORDER BY number_of_reviews DESC
--LIMIT 10;
-- Approach 3:
WITH cleaned AS (
SELECT author,
CAST(REGEXP_REPLACE(no_of_reviews, '[^0-9]', '') AS BIGINT) AS reviews
FROM bootcamp.books
)
SELECT author,
SUM(reviews) AS number_of_reviews
FROM cleaned
GROUP BY author
ORDER BY number_of_reviews DESC
--LIMIT 10