Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
-- 1 вариант select DEPARTMENT_ID, max(case when rnk = 1 then LAST_NAME else null end) as highest_paid, max(case when rnk = 2 then LAST_NAME else null end) as second_paid, max(case when rnk = 3 then LAST_NAME else null end) as third_highest from ( select DEPARTMENT_ID, LAST_NAME, dense_rank() over (partition by DEPARTMENT_ID order by salary desc) rnk from employees order by DEPARTMENT_ID )t where rnk <= 3 group by DEPARTMENT_ID order by DEPARTMENT_ID; -- 2 вариант select DEPARTMENT_ID, listagg(case when rnk = 1 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as highest_paid, listagg(case when rnk = 2 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as second_paid, listagg(case when rnk = 3 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as third_highest from ( select DEPARTMENT_ID, LAST_NAME, dense_rank() over (partition by DEPARTMENT_ID order by salary desc) rnk from employees order by DEPARTMENT_ID )t where rnk <= 3 group by DEPARTMENT_ID order by DEPARTMENT_ID

Stuck with a problem? Got Error? Ask AI support!

Copy Clear