简体   繁体   English

带条件显示的SQL Case

[英]SQL Case with conditional display

I am trying to write a query that will return 1 row for each 'Applicant' with a column that will display the employerId for the applicant if the number of employerIds is 1, otherwise it would return a string on that column saying "Multiple Applications". 我正在尝试编写一个查询,该查询将为每个“申请人”返回1行,并在该列中显示一个雇主名称(如果雇主编号为1的话),否则它将在该列上返回一个字符串,表示“多个应用程序” 。 Please see below, I am working with MSSQL. 请参见下面,我正在使用MSSQL。 Could someone guide me towards the right direction? 有人可以引导我朝正确的方向前进吗?

    Table: Applicant

    ID | FirstName | LastName
    ----------------------------
    01 | John      | Smith
    02 | Mark      | Doe
    03 | George    | Washington



    Table: Employer

    ID | ApplicantId | EmployerId
    ----------------------------
    01 | 01          | 100
    02 | 01          | 103
    03 | 02          | 101
    04 | 03          | 106


    Desired Output:

    FirstName | LastName   | Employer
    ---------------------------------
    John      | Smith      | Multiple Applications
    Mark      | Doe        | 101
    George    | Washington | 106

Thanks! 谢谢!

You can use a simple grouping and make use of MIN to get the value when there's only one item: 当只有一项时,您可以使用简单的分组并利用MIN来获取值:

SELECT Applicant.FirstName, Applicant.Surname, 
    CASE WHEN COUNT(Employer.ID) = 1 
        THEN CAST(MIN(Employer.Id) AS varchar(10))
        ELSE 'Multiple Applications' 
    END AS Employer
FROM Applicant
    INNER JOIN Employer ON Applicant.Id = Employer.Id
GROUP BY Applicant.Id, Applicant.FirstName, Applicant.Surname
select
    min(FirstName) as FirstName,
    min(LastName) as LastName,
    case
        when count(*) > 1 then 'Multiple Applications'
        else cast(min(EmployerId) as varchar(11))
    end as Employer
from Applicant as a inner join Employer as e on e.ApplicantId = a.Id
group by a.Id

It's certainly possible that you could have multiple applicants with the same name, especially just last name. 当然,您可能有多个具有相同名称的申请人,尤其是姓氏。 So you wouldn't want to group on those columns without grouping on the applicant id. 因此,您不希望在不对申请人ID进行分组的情况下对这些列进行分组。

Many people like to add extra group columns to avoid the need for a dummy aggregate expression on column from the one side of a 1:M join. 许多人喜欢添加额外的组列,以避免从1:M连接的一侧在列上使用虚拟聚合表达式。 (And other platforms allow you to refer to a constant or "functionally dependent" column without the aggregate at all.) I personally think that that approach masks the intent of the query. (和其他平台允许您引用常量或“功能相关”列,而无需汇总。)我个人认为该方法掩盖了查询的意图。 Those extra grouping could potentially change the query plan and/or performance. 这些额外的分组可能会更改查询计划和/或性能。

This case is interesting because we have to use a dummy aggregate inside the case expression anyway. 这种情况很有趣,因为无论如何我们都必须在case表达式内使用伪聚合。 So it makes that much more sense to mirror it in the first and last name columns as well. 因此,将其镜像到名字和姓氏列中也更加有意义。

Select *
 ,  CASE WHEN 
        (Select COUNT(ApplicantId) From Employer 
        Where Employer.ApplicantId = Applicant.ID) =  1 
    THEN CAST(Applicant.ID AS varchar) 
    ELSE 'Multiple Applications' ENd AS [Applicant]
From Applicant

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

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