1

我有以下代码。目的是获取 AML_TYP_CD 字段中的数据,这是他们完成的教育水平。20 和 25 与 30/35/ 40/45 相同,我需要编写一个案例语句将它们一起计数,这样 25 将返回为 20 等,然后还有大约 50 个其他教育水平代码我需要消除。最好的方法是什么,因为我正在编写的 CASE 语句不起作用。CASE 语句将进入 SELECT 子句,我写的那个不起作用的是

CASE WHEN (AML_TYP_CD) = '20' OR '25' THEN Bachelors ELSE NULL END) As Bach_Degree

其余的代码是:

    SELECT     dbo.TACCPLI.SYS_EMP_ID_NR, MAX(dbo.TACCPLI.AML_TYP_CD) AS DEG
FROM         dbo.TACCPLI INNER JOIN
                          (SELECT     dbo.v_TMS_employee_HR.GEMSID, dbo.v_TMS_employee_HR.TMS_ID
                            FROM          dbo.v_TMS_employee_HR INNER JOIN
                                                   dbo.v_sc17_TMS_Data_Career_Meeting_Rating ON 
                                                   dbo.v_TMS_employee_HR.TMS_ID = dbo.v_sc17_TMS_Data_Career_Meeting_Rating.Employee_ID
                            WHERE      (LEFT(dbo.v_sc17_TMS_Data_Career_Meeting_Rating.Meeting_Readiness_Rating, 2) IN ('14', '15')) AND 
                                                   (dbo.v_TMS_employee_HR.Job_Group_Code >= '72') AND (dbo.v_TMS_employee_HR.Job_Group_Code <= '79') AND 
                                                   (dbo.v_sc17_TMS_Data_Career_Meeting_Rating.Plan_Year = 2012) AND (dbo.v_TMS_employee_HR.Region NOT IN ('12', '13', '16', '17'))
                            GROUP BY dbo.v_sc17_TMS_Data_Career_Meeting_Rating.Meeting_Readiness_Rating, dbo.v_TMS_employee_HR.GEMSID, dbo.v_TMS_employee_HR.TMS_ID) 
                      AS HPS ON dbo.TACCPLI.SYS_EMP_ID_NR = HPS.TMS_ID
GROUP BY dbo.TACCPLI.SYS_EMP_ID_NR
4

1 回答 1

1

你的语法是错误的。你会想要这样的东西:

SELECT 
    CASE WHEN AML_TYP_CD = '20' OR AML_TYP_CD = '25' THEN 'Bachelor''s' 
         WHEN AML_TYP_CD = '30' OR AML_TYP_CD = '35' THEN 'Whatever'
         --Other cases
         WHEN AML_TYP_CD = '90' OR AML_TYP_CD = '95' THEN 'Something'
         ELSE 'Other'
    END AS Degree

或者在我看来更干净一点:

SELECT 
    CASE WHEN CONVERT(INT, AML_TYP_CD) < 30 THEN 'Bachelor''s' 
         WHEN CONVERT(INT, AML_TYP_CD) < 40 THEN 'Whatever'
         --Other cases
         WHEN CONVERT(INT, AML_TYP_CD) < 100 THEN 'Something'
         ELSE 'Other'
    END AS Degree
于 2013-01-11T16:49:21.390 回答