|
| 1 | +/* |
| 2 | +
|
| 3 | +Ketty gives Eve a task to generate a report |
| 4 | +containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those |
| 5 | +students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades |
| 6 | +are entered first. If there is more than one student with the same grade (8-10) assigned to them, order those |
| 7 | +particular students by their name alphabetically. Finally, if the grade is lower than 8, use "NULL" as their |
| 8 | +name and list them by their grades in |
| 9 | +descending order. If there is more than one student with the same grade (1-7) assigned to |
| 10 | +them, order those particular students by their marks in ascending order. |
| 11 | +
|
| 12 | +*/ |
| 13 | +with helper as ( |
| 14 | +select id, name, marks, |
| 15 | +case when marks >= 0 and marks <=9 then 1 |
| 16 | +when marks >=10 and marks <=19 then 2 |
| 17 | +when marks >=20 and marks <=29 then 3 |
| 18 | +when marks >=30 and marks <=39 then 4 |
| 19 | +when marks >=40 and marks <=49 then 5 |
| 20 | +when marks >=50 and marks <=59 then 6 |
| 21 | +when marks >=60 and marks <=69 then 7 |
| 22 | +when marks >=70 and marks <=79 then 8 |
| 23 | +when marks >=80 and marks <=89 then 9 |
| 24 | +when marks >=90 and marks <=100 then 10 |
| 25 | +end as grade |
| 26 | +from students), |
| 27 | + |
| 28 | +bads as(select null as name, grade, marks from helper where grade < 8 order by grade desc, marks), |
| 29 | + |
| 30 | +goods as(select name, grade, marks from helper where grade >= 8 order by grade desc, name), |
| 31 | + |
| 32 | +main as ( |
| 33 | +select name, grade, marks from goods |
| 34 | +union all |
| 35 | +select name, grade, marks from bads) |
| 36 | + |
| 37 | +select * from main order by grade desc, name, marks |
0 commit comments