AVG(MAX())在CASE语句ORACLE SQL中不起作用

AVG(MAX())在CASE语句ORACLE SQL中不起作用,sql,oracle,case,aggregate-functions,Sql,Oracle,Case,Aggregate Functions,您好,我正在oracle的人力资源模式中练习。我想数一数按国家和工作分组的最高工资。 当我使用 薪金 在case表达式中,它不会返回单个组函数错误 但当我单独选择它时,效果很好: `select avg(max(e.salary)) as "AVARAGE" from hr.employees e right join hr.departments d on e.department_id=d.department_id right join hr.location

您好,我正在oracle的人力资源模式中练习。我想数一数按国家和工作分组的最高工资。 当我使用 薪金 在case表达式中,它不会返回单个组函数错误

但当我单独选择它时,效果很好:

`select  avg(max(e.salary)) as "AVARAGE"
 from hr.employees e right join hr.departments d on 
 e.department_id=d.department_id
           right join hr.locations l on d.location_id=l.location_id
           right join hr.countries c on l.country_id=c.country_id
 group by c.country_id,e.job_id;`

你能解释一下如何在case语句中一起使用avgmax函数吗?提前感谢。

这里的一个快速修复方法可能是尝试在整个聚合表中使用AVG作为分析函数:

select
    c.country_id AS "COUNTRY CODE",
    e.job_id AS "JOB",
    case 
        when max(e.salary) > avg(max(e.salary)) over ()
        then 'HIGH SALARY'
        else 'LOW SALARY'
    end as info
from hr.employees e
right join hr.departments d
    on e.department_id = d.department_id
right join hr.locations l
    on d.location_id = l.location_id
right join hr.countries c
    on l.country_id = c.country_id
group by
    c.country_id,
    e.job_id;

这里的一个快速修复方法可能是尝试在整个聚合表上使用AVG作为分析函数:

select
    c.country_id AS "COUNTRY CODE",
    e.job_id AS "JOB",
    case 
        when max(e.salary) > avg(max(e.salary)) over ()
        then 'HIGH SALARY'
        else 'LOW SALARY'
    end as info
from hr.employees e
right join hr.departments d
    on e.department_id = d.department_id
right join hr.locations l
    on d.location_id = l.location_id
right join hr.countries c
    on l.country_id = c.country_id
group by
    c.country_id,
    e.job_id;
试试这个:

select t1.[COUNTRY_CODE], t1.JOB, case 
      when t1.salary > avg(t1.salary) then 'HIGH SALARY'
      else 'LOW SALARY'
   end as info 
from (select c.country_id AS "COUNTRY CODE",
       e.job_id AS "JOB",
       max(e.salary) as salary
       from
       hr.employees e right join hr.departments d on 
        e.department_id=d.department_id
               right join hr.locations l on d.location_id=l.location_id
               right join hr.countries c on l.country_id=c.country_id
        group by c.country_id,e.job_id) as t1;
group by t1.[COUNTRY_CODE], t1.JOB, t1.salary
试试这个:

select t1.[COUNTRY_CODE], t1.JOB, case 
      when t1.salary > avg(t1.salary) then 'HIGH SALARY'
      else 'LOW SALARY'
   end as info 
from (select c.country_id AS "COUNTRY CODE",
       e.job_id AS "JOB",
       max(e.salary) as salary
       from
       hr.employees e right join hr.departments d on 
        e.department_id=d.department_id
               right join hr.locations l on d.location_id=l.location_id
               right join hr.countries c on l.country_id=c.country_id
        group by c.country_id,e.job_id) as t1;
group by t1.[COUNTRY_CODE], t1.JOB, t1.salary

您可以通过一个外部选择和单独的max和avg功能来实现这一点,如下所示:

With dataa as 
(select c.country_id,
       e.job_id,
       max(e.salary) as max_salary
       from
       hr.employees e right join hr.departments d on 
        e.department_id=d.department_id
               right join hr.locations l on d.location_id=l.location_id
               right join hr.countries c on l.country_id=c.country_id
        group by c.country_id,e.job_id)

select t.country_id as COUNTRY_CODE, t.job_id as JOB, case 
          when t.max_salary > t_avg.avg_salary then 'HIGH SALARY'
          else 'LOW SALARY'
       end as info 
    from dataa t,
 (select Avg(max_salary) as avg_salary from Dataa) t_avg;

干杯

您可以通过一个外部选择并分离max和avg功能来实现这一点,如下所示:

With dataa as 
(select c.country_id,
       e.job_id,
       max(e.salary) as max_salary
       from
       hr.employees e right join hr.departments d on 
        e.department_id=d.department_id
               right join hr.locations l on d.location_id=l.location_id
               right join hr.countries c on l.country_id=c.country_id
        group by c.country_id,e.job_id)

select t.country_id as COUNTRY_CODE, t.job_id as JOB, case 
          when t.max_salary > t_avg.avg_salary then 'HIGH SALARY'
          else 'LOW SALARY'
       end as info 
    from dataa t,
 (select Avg(max_salary) as avg_salary from Dataa) t_avg;

干杯

不允许嵌套加重函数。 你应该先拿到最高工资。然后再次使用avg功能选择结果。
谢谢,

不允许嵌套加重功能。 你应该先拿到最高工资。然后再次使用avg功能选择结果。
谢谢,

它返回ORA-01747:无效的user.table.column、table.column或column规范错误[COUNTRY\u CODE]在SQL中是无效的标识符。你需要去掉那些方括号。加:该列在问题中被命名为country_id。它返回ORA-01747:无效的user.table.column、table.column或column规范错误[country_CODE]在SQL中是无效的标识符。你需要去掉那些方括号。另外:问题中的列名为country_id。这是一个case表达式。这是一个case表达式。我认为它不会返回正确答案,我用Tim Biegelsein的解决方案得到了正确答案,但我想在没有分析功能的情况下实现它。它会为每行返回“LOW SALARY”(低工资)。我认为它不会返回正确答案,我用Tim Biegelsein的解决方案得到了正确的答案,但我想在没有分析功能的情况下实现它。它会为每行返回“低工资”。谢谢,它是有效的,但在没有分析功能的情况下能否实现这些结果?上面的解决方案给了我错误的结果问题是,要知道查询分组中的平均最大值是多少,根据定义,需要迭代整个结果集一次以找到平均值。因此,如果不使用AVG OVER,则必须以这种方式子查询并找到平均值。选择t.country\u id作为国家代码,选择t.job\u id作为工作,当t.max\u salary>avgt.max\u salary时,则“高薪”或“低薪”作为信息结束,从选择c.country\u id,e.job\u id,最大工资作为人力资源部的最大工资。员工e右键加入人力资源部d on e.department\u id=d.department\u id右键加入人力资源部l on d.location\u id=l.location\u id右键加入人力资源部c on l.country\u id=c.country\u id按c.country\u id分组,e.job\u id按t.country\u id分组,t.工作id,t.最高工资;这会在每一行返回低工资,这是不正确的,但我不知道这有什么错。我推荐我目前的答案,因为替代答案将是更多的代码。谢谢,它是有效的,但不使用分析功能是否可以实现这些结果?上面的解决方案给了我错误的结果问题是,要知道查询分组中的平均最大值是多少,根据定义,需要迭代整个结果集一次以找到平均值。因此,如果不使用AVG OVER,则必须以这种方式子查询并找到平均值。选择t.country\u id作为国家代码,选择t.job\u id作为工作,当t.max\u salary>avgt.max\u salary时,则“高薪”或“低薪”作为信息结束,从选择c.country\u id,e.job\u id,最大工资作为人力资源部的最大工资。员工e右键加入人力资源部d on e.department\u id=d.department\u id右键加入人力资源部l on d.location\u id=l.location\u id右键加入人力资源部c on l.country\u id=c.country\u id按c.country\u id分组,e.job\u id按t.country\u id分组,t.工作id,t.最高工资;这会在每一行返回低工资,这是不正确的,但我不知道这有什么错。我推荐我目前的答案,因为替代方案将是更多的代码。