반응형
For this challenge you need to create a simple query to display each unique clan with their total points and ranked by their total points.
people table schema
- name
- points
- clan
You should then return a table that resembles below
select on
- rank
- clan
- total_points
- total_people
The query must rank each clan by their total_points, you must return each unqiue clan and if there is no clan name (i.e. it's an empty string) you must replace it with [no clan specified], you must sum the total_points for each clan and the total_people within that clan.
Solution:
SELECT
RANK () OVER (ORDER BY SUM(points) DESC),
CASE WHEN clan = '' THEN '[no clan specified]' ELSE clan END,
SUM(points) as total_points,
COUNT(name) as total_people
FROM
people
GROUP BY clan
Result:
rank | clan | total_points | total_people |
1 | [no clan specified] | 2398178 | 44 |
2 | none | 387976 | 1 |
3 | codewars | 344611 | 3 |
4 | 中国 长垣 | 284525 | 1 |
5 | freecodecamp | 258436 | 1 |
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
GROCERY STORE: Inventory (0) | 2022.06.20 |
---|---|
Best-Selling Books (SQL for Beginners #5) (0) | 2022.06.19 |
SQL Basics: Simple IN (0) | 2022.06.17 |
Easy SQL: Absolute Value and Log to Base (0) | 2022.06.16 |
SQL Basics: Create a FUNCTION (0) | 2022.06.15 |