将串联查询添加到现有代码

时间:2018-12-13 21:12:25

标签: sql sql-server concatenation

我有以下代码:

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'
          WHEN CONC.Clinical like '%level 3%' THEN '99283'
          WHEN CONC.Clinical like '%level 2%'THEN '99282'
          WHEN CONC.Clinical like '%level 1%' THEN '99281'
   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)
          WHEN CONC.Clinical like '%level 3%' THEN CAST($3 AS int)
          WHEN CONC.Clinical like '%level 2%'THEN CAST($2 AS int)
          WHEN CONC.Clinical like '%level 1%' THEN CAST ($1AS 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)
          WHEN CPT.CPT like '99283' THEN CAST($3 AS int)
          WHEN CPT.CPT like '99282'THEN CAST($2 AS int)
          WHEN CPT.CPT like '99281' THEN CAST($1 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

我如何在上面的代码中包括其他代码(或有其他选择)以解析列,以便将每个临床事件转换成其自己的列,所需的输出如下所示:

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

1 个答案:

答案 0 :(得分:1)

您可以使用CASE表达式。

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