简体   繁体   English

多条件选择语句

[英]Multiple condition select statement

I've written some code (below) which is trying to select a Job Title and the number of employees assigned to the job title, if it's OrganizationLevel is beneath 3. 我编写了一些代码(如下),如果组织级别低于3,它试图选择职位和分配给该职位的雇员人数。

I am then trying to insert another couple of conditions. 然后,我试图插入另外几个条件。 When the OrganizationLevel is beneath 2, the Job Title becomes upper case. 当OrganizationLevel低于2时,职位名称变为大写。

But I am struggling to implement a second condition in to this SQL. 但是我正在努力在此SQL中实现第二个条件。 When the OrganizationLevel = 1, the Job Title should display 'MANAGING DIRECTOR'. 当OrganizationLevel = 1时,职位名称应显示“ MANAGING DIRECTOR”。

Can someone please help me in achieving this? 有人可以帮我实现这一目标吗? Here's what I have so far: 这是我到目前为止的内容:

with T as( SELECT IIF ( OrganizationLevel < 2, UPPER ( IIF ( OrganizationLevel=1, 'MANAGING DIRECTOR', JobTitle )), JobTitle ) AS JobTitle , OrganizationLevel
from HumanResources.Employee)
select count(*) AS NumOfEmp,JobTitle from T 
where OrganizationLevel < 3 group by JobTitle order by JobTitle;

Thank you. 谢谢。

You should try the CASE expression: 您应该尝试使用CASE表达式:

with T as( 
SELECT case
        when OrganizationLevel = 1 then 'MANAGING DIRECTOR'
        when OrganizationLevel < 2 then upper(JobTitle)
        else JobTitle 
    end AS JobTitle,
    OrganizationLevel
from HumanResources.Employee)

select count(*) AS NumOfEmp, JobTitle 
from T 
where OrganizationLevel < 3 
group by JobTitle 
order by JobTitle;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM