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