如何在不重复group by子句的第一列的情况下获取聚合计数?

时间:2013-04-17 00:27:36

标签: sql-server-2008 tsql group-by pivot i2b2

这个查询强制我把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

enter image description here

1 个答案:

答案 0 :(得分: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

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

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