Sql I';I’我想知道哪个国家的薪水最高

Sql I';I’我想知道哪个国家的薪水最高,sql,oracle11g,oracle-sqldeveloper,Sql,Oracle11g,Oracle Sqldeveloper,您希望只显示中间结果的最佳行。从Oracle 12c开始,您可以使用fetchfirst n ROWS子句进行此操作。在Oracle 11g之前,您应该改用窗口函数: select last_name, country_name, SUM(salary) from employees e JOIN departments d ON (e.department_id= d.department_id) JOIN locations L ON (d.location_id = L.location

您希望只显示中间结果的最佳行。从Oracle 12c开始,您可以使用
fetchfirst n ROWS
子句进行此操作。在Oracle 11g之前,您应该改用窗口函数:

select last_name, country_name, SUM(salary)
from employees e JOIN departments d ON (e.department_id= d.department_id) 
JOIN locations L ON (d.location_id = L.location_id) 
JOIN Countries Cc ON (L.country_id = Cc.country_id) 
JOIN regions Rr ON (Cc.region_id = Rr.region_id) 
GROUP BY country_name;

你发布的代码无法编译;它在
groupby
中缺少
LAST\u NAME
(这基本上是错误的,因为这会使您无法完成所做的事情),或者-更好的办法是-将其从
SELECT
语句的列列表中删除

使用
RANK
分析函数

SELECT country_name, sum_salary
FROM
(
  select 
    c.country_name,
    SUM(e.salary) AS sum_salary,
    MAX(SUM(e.salary)) OVER () AS max_sum_salary
  from employees e 
  JOIN departments d ON e.department_id = d.department_id
  JOIN locations l ON d.location_id = l.location_id
  JOIN countries c ON l.country_id = c.country_id
  GROUP BY c.country_name
)
WHERE sum_salary = max_sum_salary
ORDER BY country_name;

我没有您的表或数据,因此-为了说明-我将使用Scott的示例模式。简而言之,它是这样的:

WITH data
     AS (  SELECT country_name,
                  SUM (salary) sumsal,
                  RANK () OVER (ORDER BY SUM (salary) DESC) rn
             FROM employees e
                  JOIN departments d ON (e.department_id = d.department_id)
                  JOIN locations L ON (d.location_id = L.location_id)
                  JOIN Countries Cc ON (L.country_id = Cc.country_id)
                  JOIN regions Rr ON (Cc.region_id = Rr.region_id)
         GROUP BY country_name)
SELECT country_name, sumsal
  FROM data
 WHERE rn = 1;
因此:


编辑问题并添加一些示例数据&期望的结果会有所帮助。
SQL> select deptno, sum(sal)
  2  from emp
  3  group by deptno
  4  order by 2 desc;

    DEPTNO   SUM(SAL)
---------- ----------
        10      13750        --> this is a department you need
        20      10995
        30       9400
SQL> WITH data
  2       AS (  SELECT deptno,
  3                    SUM (sal) sumsal,
  4                    RANK () OVER (ORDER BY SUM (sal) DESC) rn
  5               FROM emp
  6           GROUP BY deptno)
  7  SELECT deptno, sumsal
  8    FROM data
  9   WHERE rn = 1;

    DEPTNO     SUMSAL
---------- ----------
        10      13750

SQL>