我坚持使用CASE / WHEN声明
我需要做的是检查HOSP_DISCHRG_TIME
中是否找到了值 - 如果是,则在日期差异计算中使用HOSP_DISCHRG_TIME
,否则使用当前日期。
这是我写的,但似乎不起作用。关于这种方法或其他方法的任何建议
由于 史蒂夫
-- Calculate L
CASE
WHEN HOSP_DISCHRG_TIME is NULL
THEN
CASE
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) < 1 THEN 0
When ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0)
ELSE
CASE
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) < 1 THEN 0
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0)
END
END AS L,
答案 0 :(得分:1)
我认为你只是错过了一个结束 -
-- Calculate L
CASE
WHEN HOSP_DISCHRG_TIME is NULL
THEN
CASE
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) < 1 THEN 0
When ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,GETDATE()),0)
END -- Added this
ELSE
CASE
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) < 1 THEN 0
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,PAT_ENC.HOSP_ADMSN_TIME,HOSP_DISCHRG_TIME),0)
END -- Fixed indentation
END AS L, -- Fixed indentation
因为这有效(它只是替换了我声明的变量而无法看到的列):
DECLARE @HOSP_DISCHRG_TIME datetime
DECLARE @HOSP_ADMSN_TIME datetime
SET @HOSP_ADMSN_TIME = GETDATE() - 4
SELECT CASE
WHEN @HOSP_DISCHRG_TIME is NULL
THEN
CASE
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,GETDATE()),0) < 1 THEN 0
When ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,GETDATE()),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,GETDATE()),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,GETDATE()),0)
END
ELSE
CASE
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,@HOSP_DISCHRG_TIME),0) < 1 THEN 0
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,@HOSP_DISCHRG_TIME),0) BETWEEN 4 and 6 THEN 4
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,@HOSP_DISCHRG_TIME),0) BETWEEN 7 and 13 THEN 5
WHEN ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,@HOSP_DISCHRG_TIME),0)>= 14 THEN 6
ELSE ISNULL(DATEDIFF(dd,@HOSP_ADMSN_TIME,@HOSP_DISCHRG_TIME),0)
END
END AS L