1

这个查询迫使我将 f.CONCEPT_CD 放在 group by 子句中,因为我在 select 子句的 case 语句中使用了它。如何在结果集中保留计数,而不用相同的 f.PATIENT_NUM 重复记录?请注意在下面的屏幕截图中如何重复 PATIENT_NUM = 5。如果 PATIENT_NUM = 5,我只想显示一条记录。

SELECT 
    f.PATIENT_NUM,  
    CASE WHEN f.CONCEPT_CD = 'BIO|HGT' THEN COUNT(NVAL_NUM) ELSE 0 END AS 'HEIGHT', 
    CASE WHEN f.CONCEPT_CD = 'BIO|WGT' THEN COUNT(NVAL_NUM) ELSE 0 END AS 'WEIGHT', 
    CASE WHEN f.CONCEPT_CD = 'BIO|BMI' THEN COUNT(NVAL_NUM) ELSE 0 END AS 'BMI', 
    CASE WHEN f.CONCEPT_CD = 'BIO|DIA' THEN COUNT(NVAL_NUM) ELSE 0 END AS 'DIA', 
    CASE WHEN f.CONCEPT_CD = 'BIO|SYS' THEN COUNT(NVAL_NUM) ELSE 0 END AS 'SYS' 
  FROM [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_OBSERVATION_FACT] f
  JOIN [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_CONCEPT_DIMENSION] d ON f.CONCEPT_CD = d.CONCEPT_CD 
  GROUP BY f.PATIENT_NUM, f.CONCEPT_CD

在此处输入图像描述

4

1 回答 1

3
SELECT  f.PATIENT_NUM,  
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|HGT' THEN 1 END) AS 'HEIGHT', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|WGT' THEN 1 END) AS 'WEIGHT', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|BMI' THEN 1 END) AS 'BMI', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|DIA' THEN 1 END) AS 'DIA', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|SYS' THEN 1 END) AS 'SYS' 
FROM    [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_OBSERVATION_FACT] f
        INNER JOIN [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_CONCEPT_DIMENSION] d 
            ON f.CONCEPT_CD = d.CONCEPT_CD 
GROUP   BY f.PATIENT_NUM

您也可以使用PIVOT()sql server 支持的,

SELECT  PATIENT_NUM,
        [BIO|HGT] AS 'HEIGHT',
        [BIO|WGT] AS 'WEIGHT',
        [BIO|BMI] AS 'BMI',
        [BIO|DIA] AS 'DIA',
        [BIO|SYS] AS 'SYS'
FROM
    (
        SELECT  f.PATIENT_NUM, f.CONCEPT_CD
        FROM    [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_OBSERVATION_FACT] f
                INNER JOIN [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_CONCEPT_DIMENSION] d 
                    ON f.CONCEPT_CD = d.CONCEPT_CD 
    ) org
    PIVOT
    (
        COUNT(*)
        FOR PATIENT_NUM IN ([BIO|HGT],[BIO|WGT],[BIO|BMI],
                            [BIO|DIA],[BIO|SYS])
    ) pvt

更新 1

SELECT  f.PATIENT_NUM,  
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|HGT' THEN 1 END) AS 'HEIGHT', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|WGT' THEN 1 END) AS 'WEIGHT', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|BMI' THEN 1 END) AS 'BMI', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|DIA' THEN 1 END) AS 'DIA', 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|SYS' THEN 1 END) AS 'SYS' 
FROM    [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_OBSERVATION_FACT] f
        INNER JOIN [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_CONCEPT_DIMENSION] d 
            ON f.CONCEPT_CD = d.CONCEPT_CD 
GROUP   BY f.PATIENT_NUM
HAVING  COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|HGT' THEN 1 END) >  0 AND
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|WGT' THEN 1 END) >  0 AND 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|BMI' THEN 1 END) >  0 AND 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|DIA' THEN 1 END) >  0 AND 
        COUNT(CASE WHEN f.CONCEPT_CD = 'BIO|SYS' THEN 1 END) >  0 
于 2013-04-17T00:31:01.590 回答