Sql 2012简化查询联盟

时间:2015-08-21 13:05:20

标签: sql left-join union

我最近接受了一份新工作,我正在尝试简化剩下的一些旧查询,对于我的生活,我无法弄清楚如何使用union将其分解为两个查询。必须有一种方法,但我不能简化比我所拥有的更多。

    select  (
    select avg (Employee.salary)
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='0'
    group by Employee.Exempt_Non_Exempt_Status
    ) as AverageSalary, 'Non-Exempt' as Status
    ,       (
    select MIN (Employee.salary)
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='0'
    group by Employee.Exempt_Non_Exempt_Status
    ) as MinimumSalary, 'Non-Exempt' as Status
    ,       (
    select MAX (Employee.salary)
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='0'
    group by Employee.Exempt_Non_Exempt_Status
    ) as MaximumSalary, 'Non_Exempt' as Status
 UNION
    select  (
    select avg (Employee.salary)
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='1'
    group by Employee.Exempt_Non_Exempt_Status
    ) as AverageSalary, 'Exempt' as Status
    ,       (
    select MIN (Employee.salary)
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='1'
    group by Employee.Exempt_Non_Exempt_Status
    ) as MinimumSalary, 'Exempt' as Status
    ,       (
    select MAX (Employee.salary) 
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='1'
    group by Employee.Exempt_Non_Exempt_Status
    )as MaximumSalary, 'Exempt' as Status

3 个答案:

答案 0 :(得分:1)

由于您只在每个子查询中选择了1个字段,因此您不需要" Group By"。此外,根据数据,您可能不需要在Job_title表中进行链接。

如果您需要Job_title表,那么......

SELECT Min(salary), Max(salary), Avg(salary), 'Non-Exempt' AS Status 
FROM   (SELECT employee.salary 
        FROM   employee 
                LEFT JOIN job_title  ON employee.job_title = job_title.job_title 
        WHERE  employee.exempt_non_exempt_status = '0') 
UNION 
SELECT Min(salary), Max(salary), Avg(salary), 'Exempt' AS Status 
FROM   (SELECT employee.salary 
        FROM   employee 
               LEFT JOIN job_title 
                      ON employee.job_title = job_title.job_title 
        WHERE  employee.exempt_non_exempt_status = '1') 

如果您不需要在Job_title中链接(因为您未在任何地方使用它),那么......

SELECT Min(salary), Max(salary), Avg(salary), 'Non-Exempt' AS Status 
FROM   (SELECT employee.salary 
        FROM   employee 
        WHERE  employee.exempt_non_exempt_status = '0') 
UNION 
SELECT Min(salary), Max(salary), Avg(salary), 'Exempt' AS Status 
FROM   (SELECT employee.salary 
        FROM   employee 
        WHERE  employee.exempt_non_exempt_status = '1') 

答案 1 :(得分:0)

你应该可以这样做:

select min(salary), max(salary), avg(salary), 'Non-Exempt' as otherThingy
from (
    select Employee.salary
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='0'
) thingy
UNION
select min(salary), max(salary), avg(salary), 'Exempt' as otherThingy
from (
    select Employee.salary
    from Employee left join Job_title
    on Employee.job_title = Job_title.Job_title
    where Employee.Exempt_Non_Exempt_Status='1'
) thingy

或者为了得到真正的幻想,你最有可能一次性完成这一切:

http://sqlfiddle.com/#!6/8b437/1

select 
    min(salary), 
    max(salary), 
    avg(salary), 
    case when Exempt_Non_Exempt_Status = 0 
        then 'Non-Exempt' 
        else 'Exempt' 
    end as exemptStatus
from (
    select Employee.salary, Exempt_Non_Exempt_Status
    from Employee
) thingy
group by Exempt_Non_Exempt_Status

答案 2 :(得分:0)

感谢大家的快速反应。这是我跟的代码。我能够使用所提供的所有代码获得相同的结果,并且想要感谢所有人。

SELECT Min(salary) as 'Minimum Salary', Max(salary) as 'Maximum Salary',Avg(salary) as 'Average Salary', 'Non-Exempt' AS Status 
FROM   (SELECT employee.salary 
    FROM   employee 
    WHERE  employee.exempt_non_exempt_status = '0') thingy1
UNION 
SELECT Min(salary) as 'Minimum Salary', Max(salary) as 'Maximum Salary', Avg(salary) as 'Average Salary', 'Exempt' AS Status 
FROM   (SELECT employee.salary 
    FROM   employee 
    WHERE  employee.exempt_non_exempt_status = '1') thingy2