巢案陈述

时间:2018-12-14 01:53:14

标签: sql sql-server case

我有以下代码:

USE EMTCQIData

SELECT FIN, Clinical, FCT_MAX, FCT_MAX_CHARGES, CPT, CPT_CHARGES, 
(FCT_MAX_CHARGES - CPT_CHARGES) AS DELTA

FROM
(
SELECT CONC.[Financial Number] as FIN, CONC.Clinical, 

CASE
      WHEN CONC.Clinical like '%level 5%' THEN '99285' 
      WHEN CONC.Clinical like '%level 4%' THEN '99284'
END FCT_MAX,

CASE
      WHEN CONC.Clinical like '%level 5%' THEN CAST($200 AS int) 
      WHEN CONC.Clinical like '%level 4%' THEN CAST($100 AS int)
END FCT_MAX_CHARGES,

CPT.CPT,

CASE
      WHEN CPT.CPT like '99285' THEN CAST($200 AS int) 
      WHEN CPT.CPT like '99284' THEN CAST ($100 AS int)
END CPT_CHARGES

FROM CPT_MASTER as CPT

INNER JOIN 

 (
SELECT DISTINCT ST2.[Financial Number], 
SUBSTRING(
(
    SELECT ','+ST1.[Clinical Event Result]  AS [text()]
    FROM ED_FCT_Q1_FY19 ST1
    WHERE ST1.[Financial Number] = ST2.[Financial Number] AND ST1. 
 [Clinical Event Result]<> 'No'
          Order BY [Financial Number] 
    FOR XML PATH ('')
), 2, 1000) [Clinical]
 FROM ED_FCT_Q1_FY19 ST2
)
as CONC ON CONC.[Financial Number]=CPT.FIN
WHERE CPT.CPT LIKE '9928%'
)
AS COMPLETE

ORDER BY FIN 

此代码产生下表:

FIN   Clinical                              FCT_MAX    FCT_MAX_Charges    CPT   CPT_Charges    DELTA 
1     Discharge[Level1], Transfer[Leve3]        9284       200          9283     100          100
2      Vital[Level1],Triage [Level4]            9284       200          9282     50          150

我想添加一个嵌套的CASE表达式来解析CLINICAL列;但是我不确定在哪里嵌套CASE表达式。 CASE表达式将转换表,以便在CLINICAL列中有特定值时创建新列。

CASE代码如下:

CASE WHEN Clinical LIKE '%Discharge%' THEN 1 ELSE 0 END AS Discharge
etc...

我希望最终的期望结果如下所示:

FIN   Discharge  Transfer  Triage  Vital   FCT_MAX    FCT_MAX_Charges    CP CPT_Charges    DELTA 
1      1          1         0       0          9284       200           9283     100          100
2      0          0         1       1          9284       200           9282     50          150

2 个答案:

答案 0 :(得分:1)

您可以尝试将条件加重函数SUMCASE WHEN一起使用

SELECT FIN,
    SUM(CASE WHEN Clinical LIKE '%Discharge%' THEN 1 ELSE 0 END) Discharge,
    SUM(CASE WHEN Clinical LIKE '%Transfer%' THEN 1 ELSE 0 END) Transfer,
    SUM(CASE WHEN Clinical LIKE '%Triage%' THEN 1 ELSE 0 END) Triage,
    SUM(CASE WHEN Clinical LIKE '%Vital%' THEN 1 ELSE 0 END) Vital,
    FCT_MAX,
    FCT_MAX_Charges,
    CPT,
    CPT_Charges 
FROM T
GROUP BY 
    FIN,
    FCT_MAX,
    FCT_MAX_Charges,
    CPT,
    CPT_Charges

SQLFIDDLE

注意

使用您当前使用的子查询代替T

答案 1 :(得分:1)

USE EMTCQIData

SELECT FIN
  --, Clinical
  , CASE WHEN Clinical LIKE '%Discharge%' THEN 1 ELSE 0 END AS Discharge
  , CASE WHEN Clinical LIKE '%Transfer%' THEN 1 ELSE 0 END AS Transfer
  , CASE WHEN Clinical LIKE '%Triage%' THEN 1 ELSE 0 END AS Triage
  , CASE WHEN Clinical LIKE '%Vital%' THEN 1 ELSE 0 END AS Vital
  , FCT_MAX, FCT_MAX_CHARGES, CPT, CPT_CHARGES, 
(FCT_MAX_CHARGES - CPT_CHARGES) AS DELTA

FROM
(
SELECT CONC.[Financial Number] as FIN, CONC.Clinical, 

CASE
      WHEN CONC.Clinical like '%level 5%' THEN '99285' 
      WHEN CONC.Clinical like '%level 4%' THEN '99284'
END FCT_MAX,

CASE
      WHEN CONC.Clinical like '%level 5%' THEN CAST($200 AS int) 
      WHEN CONC.Clinical like '%level 4%' THEN CAST($100 AS int)
END FCT_MAX_CHARGES,

CPT.CPT,

CASE
      WHEN CPT.CPT like '99285' THEN CAST($200 AS int) 
      WHEN CPT.CPT like '99284' THEN CAST ($100 AS int)
END CPT_CHARGES

FROM CPT_MASTER as CPT

INNER JOIN 

 (
SELECT DISTINCT ST2.[Financial Number], 
SUBSTRING(
(
    SELECT ','+ST1.[Clinical Event Result]  AS [text()]
    FROM ED_FCT_Q1_FY19 ST1
    WHERE ST1.[Financial Number] = ST2.[Financial Number] AND ST1. 
 [Clinical Event Result]<> 'No'
          Order BY [Financial Number] 
    FOR XML PATH ('')
), 2, 1000) [Clinical]
 FROM ED_FCT_Q1_FY19 ST2
)
as CONC ON CONC.[Financial Number]=CPT.FIN
WHERE CPT.CPT LIKE '9928%'
)
AS COMPLETE

ORDER BY FIN