如何在sql developer的同一列中选择max / min值

时间:2014-11-03 00:47:25

标签: sql join max oracle-sqldeveloper min

我创建了一个视图(a7t3step1),列OID(办公室ID),HIGHEST(指最高工资)和LOWEST(指最低工资)(此信息来自员工表)。

我正在尝试创建第二个视图(a7t3),该视图显示EMPLOYEE_NAME,OFFICE_NAME,JOB_TITLE和SALARY列(此信息来自office和employee表)。

对于SALARY列,我应该从第一个视图到SALARY列(不尝试连接它们)选择最低和最高工资(应该是10行,每个最多/最少5个办公室)

这是我到目前为止,我不知道如何将LOWEST薪水添加到第二个视图的SALARY列。根据我在下面创建的视图,我只能看到5行(每个办公室的最高工资)。我已经尝试了“在employee.salary = a7t3step1.highest上加入a7t3step1”之后的“内部加入a7t3step1 on employee.salary = a7t3step1.lowest”但是当我尝试“从a7t3中选择*”时,我看到0行

将视图a7t3创建为

选择fname || ''|| lname为employee_name,officename为office_name,jobtitle为job_title,salary

来自办公室内部联合员工on office.oid = employee.oid inner加入a7t3step1 on employee.salary = a7t3step1.highest

按lname,fname;

排序

1 个答案:

答案 0 :(得分:0)

要获得每个办公室的最低和最高工资,您可以进行以下查询。

select o.oid, o.officename as office_name, min(e.salary) as min_salary, max(e.salary) as max_salary
FROM office o
join employee e on e.oid = o.oid
group by o.oid, o.officename

如果您想将其包含在上面的视图中,请使用上面的查询加入员工。您无法从同一个查询中获取员工姓名,因为您无法通过办公室获得工资。

select emp.fname || ' ' || emp.lname as employee_name, emp.jobtitle as job_title, 
off.office_name, off.min_salary || ' - ' || off.max_salary
from employee emp
join (select o.oid, o.officename as office_name, 
    min(e.salary) as min_salary, max(e.salary) as max_salary
    FROM office o
    join employee e on e.oid = o.oid
    group by o.oid, o.officename) off
on off.oid = emp.oid