sql - I need to retrieve the department, which has the max(avg(salary)) for each region from table hr (oracle) -


i need retrieve highest average salary group regions table hr (oracle) output should be: region, department_name, max(avg(salary)) can't output region , department_name together

    select t1.region_name, max(t1.avg)from (     select         region_name,         department_name,         round(avg(salary) ) avg             regions reg         inner join countries coun on reg.region_id = coun.region_id         inner join locations loc on coun.country_id = loc.country_id         inner join departments dep on loc.location_id = dep.location_id         inner join employees emp on emp.department_id = dep.department_id     group         region_name,         department_name     order 3 ) t1group t1.region_name 

i suppose looking department(s) per region have highest avarage salary in region. you'd rank joined rows avarage salary , keep highest ranked rows:

select   region_name,   department_name,   avg_salary (   select     reg.region_name,     dep.department_name,     avg(emp.salary) avg_salary,     rank() on (partition reg.region_name order avg(emp.salary) desc) rnk   regions reg     inner join countries coun on reg.region_id = coun.region_id     inner join locations loc on coun.country_id = loc.country_id     inner join departments dep on loc.location_id = dep.location_id     inner join employees emp on emp.department_id = dep.department_id   group reg.region_name, dep.department_name ) rnk = 1 order region_name, department_name; 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -