你可以在CASE上做一个ELSE吗?

时间:2011-11-03 20:37:07

标签: sql

CASE WHEN P.NURSING_UNIT is not null THEN P.NURSING_UNIT ELSE '' END NURSING_UNIT
    ,CASE WHEN P.UNIT_CODE is not null THEN P.UNIT_CODE ELSE '' END UNIT_CODE, 
    CASE WHEN M.SIGN_DATE IS NOT NULL THEN 'COMPLETED' ELSE 
    WHEN M.SIGN_DATE IS NULL THEN 'UNCOMPLETED' AS ASSESSMENTS

错误:因为sp现在没有编译。收到此错误消息:         消息156,级别15,状态1,过程GET_SCHEDULE_ALL_DETAIL,第18行         关键字“WHEN”附近的语法不正确。         消息156,级别15,状态1,过程GET_SCHEDULE_ALL_DETAIL,第25行         关键字“AND”附近的语法不正确。

    ----

    USE [PRO]
    GO

    /****** Object: StoredProcedure [dbo].[GET_SCHEDULE_ALL_DETAIL] Script Date: 11/02/2011 14:14:50 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO


    alter PROC [dbo].[GET_SCHEDULE_ALL_DETAIL]
    @FACILITYKEY varchar(1000),
    @UNITSTR VARCHAR(100),
    @FromDate datetime,
    @ToDate datetime
    AS
    BEGIN

    (SELECT S.FACILITY_KEY, S.PAT_NUMBER, S.A3A_DATE_USER, M.REFERENCE_DATE ,
    RTRIM(P.LAST_NAME) + CASE WHEN RTRIM(P.FIRST_NAME) <> '' THEN ', '
    ELSE ''
    END + RTRIM(P.FIRST_NAME) PATIENT_NAME
    ,CASE WHEN P.NURSING_UNIT is not null THEN P.NURSING_UNIT ELSE '' END NURSING_UNIT
    ,CASE WHEN P.UNIT_CODE is not null THEN P.UNIT_CODE ELSE '' END UNIT_CODE, 
    CASE WHEN M.SIGN_DATE IS NOT NULL THEN 'COMPLETED' ELSE 
    WHEN M.SIGN_DATE IS NULL THEN 'UNCOMPLETED' AS ASSESSMENTS
    FROM [PC].MDS_M_SCHEDULE S INNER JOIN OPTC.MD3_M_MAST M
    ON S.PAT_NUMBER=M.PAT_NUMBER
    LEFT JOIN OGEN.GEN_M_PATIENT_MAST P ON S.PAT_NUMBER = P.PAT_NUMBER
    WHERE S.PAT_NUMBER=M.PAT_NUMBER AND M.REFERENCE_DATE < GETDATE()
    AND S.A3A_DATE_USER BETWEEN @FromDate AND @ToDate
    AND S.FACILITY_KEY IN (SELECT Value FROM dbo.ListToTable(@FACILITYKEY,','))
    AND ( @UNITSTR IS NULL
    OR @UNITSTR = ''
    OR CHARINDEX(P.UNIT_CODE, @UNITSTR)% 2 = 1 ))

    UNION ALL

    (SELECT * FROM (
    (SELECT S.FACILITY_KEY, S.PAT_NUMBER, S.A3A_DATE_USER, M.REFERENCE_DATE ,
    RTRIM(P.LAST_NAME) + CASE WHEN RTRIM(P.FIRST_NAME) <> '' THEN ', '
    ELSE ''
    END + RTRIM(P.FIRST_NAME) PATIENT_NAME
    ,CASE WHEN P.NURSING_UNIT is not null THEN P.NURSING_UNIT ELSE '' END NURSING_UNIT
    ,CASE WHEN P.UNIT_CODE is not null THEN P.UNIT_CODE ELSE '' END UNIT_CODE, 'LATE' AS ASSESSMENTS
    FROM [PC].MDS_M_SCHEDULE S INNER JOIN OPTC.MD3_M_MAST M
    ON S.PAT_NUMBER=M.PAT_NUMBER
    LEFT JOIN OGEN.GEN_M_PATIENT_MAST P ON S.PAT_NUMBER = P.PAT_NUMBER
    WHERE M.REFERENCE_DATE < GETDATE() AND S.A3A_DATE_USER BETWEEN @FromDate AND @ToDate
    AND ( @UNITSTR IS NULL
    OR @UNITSTR = ''
    OR CHARINDEX(P.UNIT_CODE, @UNITSTR)% 2 = 1 )
    ) --Started

    UNION ALL

    (SELECT S.FACILITY_KEY, S.PAT_NUMBER, S.A3A_DATE_USER, NULL AS REFERENCE_DATE,
    RTRIM(P.LAST_NAME) + CASE WHEN RTRIM(P.FIRST_NAME) <> '' THEN ', '
    ELSE ''
    END + RTRIM(P.FIRST_NAME) PATIENT_NAME
    ,CASE WHEN P.NURSING_UNIT is not null THEN P.NURSING_UNIT ELSE '' END NURSING_UNIT
    ,CASE WHEN P.UNIT_CODE is not null THEN P.UNIT_CODE ELSE '' END UNIT_CODE, 'LATE' AS ASSESSMENTS
    FROM [PC].MDS_M_SCHEDULE S INNER JOIN OPTC.MD3_M_MAST M
    ON S.PAT_NUMBER=M.PAT_NUMBER
    LEFT JOIN OGEN.GEN_M_PATIENT_MAST P ON S.PAT_NUMBER = P.PAT_NUMBER
    WHERE S.PAT_NUMBER NOT IN (SELECT M.PAT_NUMBER FROM [PC].MD3_M_MAST M)
    AND S.A3A_DATE_USER < GETDATE() AND S.A3A_DATE_USER BETWEEN @FromDate AND @ToDate
    AND ( @UNITSTR IS NULL
    OR @UNITSTR = ''
    OR CHARINDEX(P.UNIT_CODE, @UNITSTR)% 2 = 1 )) --Not Started
    ) LATE
    WHERE FACILITY_KEY IN (SELECT Value FROM dbo.ListToTable(@FACILITYKEY,',')))

    END

    GO

4 个答案:

答案 0 :(得分:9)

不,ELSE是一个全能的产品。在您的示例中,不清楚为什么要在ELSE子句中包含条件,因为您已经在第一个WHEN表达式中检查了逻辑相反的条件。

但是,更一般地说,您可以嵌套CASE表达式,如下所示:

CASE
  WHEN m.sign_date IS NOT NULL THEN 'COMPLETED'
  ELSE
    CASE WHEN m.start_date IS NOT NULL THEN 'IN PROGRESS' ELSE 'NOT STARTED' END
END

答案 1 :(得分:3)

我认为您可以在此之前删除其他内容,并在“作为评估”之前添加结尾

   coalesce(P.NURSING_UNIT , '') NURSING_UNIT,  -- you can use coalesce here too
   coalesce(P.UNIT_CODE, '') UNIT_CODE, 
   CASE 
       WHEN M.SIGN_DATE IS NOT NULL THEN 'COMPLETED'  
       WHEN M.SIGN_DATE IS NULL THEN 'UNCOMPLETED' END AS ASSESSMENTS

答案 2 :(得分:3)

更改此部分:

CASE WHEN M.SIGN_DATE IS NOT NULL THEN 'COMPLETED' ELSE 
    WHEN M.SIGN_DATE IS NULL THEN 'UNCOMPLETED'

CASE WHEN M.SIGN_DATE IS NOT NULL THEN 'COMPLETED' ELSE 'UNCOMPLETED' END

if (true) { ...} else if (false) { ... }没有意义。只要让其他人无条件。

答案 3 :(得分:1)

标准SQL:

COALESCE(P.NURSING_UNIT, '') AS NURSING_UNIT, 
COALESCE(P.UNIT_CODE, '') AS UNIT_CODE, 
CASE 
   WHEN M.SIGN_DATE IS NULL THEN 'UNCOMPLETED' 
   ELSE 'COMPLETED' 
END AS ASSESSMENTS

如果您的供应商提供REPLACE()功能:

COALESCE(P.NURSING_UNIT, '') AS NURSING_UNIT, 
COALESCE(P.UNIT_CODE, '') AS UNIT_CODE, 
COALESCE(REPLACE(M.SIGN_DATE, M.SIGN_DATE, 'COMPLETED'), 'UNCOMPLETED') AS ASSESSMENTS