子查询个案声明

时间:2018-12-13 15:59:09

标签: sql sql-server sql-server-2012 subquery case

步骤1:

我有以下代码生成下表:

USE EMTCQIData

SELECT * , (FCT_MAX_CHARGES - CPT_CHARGES) AS DELTA
FROM
    (
    SELECT CONC.[FIN] as FIN, CONC.Clinical, 
        CASE
          WHEN CONC.Clinical like '%level 5%' THEN '9285' 
        END FCT_MAX,
        CASE
          WHEN CONC.Clinical like '%level 5%' THEN CAST($200 AS int) 
        END FCT_MAX_CHARGES,
        CPT.CPT,
        CASE
          WHEN CPT.CPT like '9285' THEN CAST($200 AS int) 
        END CPT_CHARGES
    FROM CPT_MASTER as CPT
    INNER JOIN 
    (
        SELECT DISTINCT ST2.[FIN], 
            SUBSTRING(
              (
                SELECT ','+ST1.[Clinical Event Result]  AS [text()]
                FROM ED_FCT_Q1_FY19 ST1
                WHERE ST1.[FIN] = ST2.[Financial Number] AND ST1.[FIN]<> '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

生产表(结果表1)

FIN   Clinical                           FCT_MAX   FCT_MAX_Charges    CPT  CPT_Charges  DELTA 
1     Dicharge[Level2] Transfer [Level3]  9284       200             9283    100        100

第2步:

我有一个CASE代码,可根据临床事件的级别(不包含任何CPT / FCT数据,因为它不在ED_FCT_Q1_FY19表中)来解析“临床”列。因此,例如,除了将“临床”列替换为各个级别的列之外,上面产生的结果看起来相同。

如何将此CASE代码嵌套在上面的代码中,以便在下面生成所需的表。

所需表

FIN   Level 1  Level 3             FCT_MAX   FCT_MAX_Charges    CPT  CPT_Charges  DELTA 
1     Dicharge Transfer            9284       200             9283    100        100

我用来从结果表1中解析CLINICAL COLUMN的案例代码示例

SELECT [Financial Number], [Clinical Event Result],

CASE
   WHEN [Clinical Event Result] like '%1:1 Nursing care%' Then '1'
      ELSE '0'
End [1:1 Nursing Care],

CASE
   WHEN [Clinical Event Result] like '%Admission or O.R. preparation%' Then '1'
      ELSE '0'
End [Admission or O.R. preparation],
FROM ED_FCT_Q1_FY19

0 个答案:

没有答案