我正在使用Microsoft SQL Server Management Studio 2008.
我有一个Blood_Pressure_Systolic字段和一个Blood_Pressure_Diastolic字段。我需要根据梅奥诊所的类别,使用这两个字段来识别正常血压,高血压前期,1期高血压和2期高血压:Mayo Clinic Blood Pressure Chart
我目前正在使用下面的CASE表达式来过滤每个类别的收缩压范围和舒张压范围。但是,如果一个人的收缩压在较低的范围内,而舒张血压在较高的范围内,我想要返回更高的范围,反之亦然。例如,如果约翰史密斯有125收缩期(高血压前期)和95舒张期(1期高血压),那么我希望该领域返回他有1期高血压。
CASE WHEN BP_Systolic < '120' AND BP_Diastolic < '80' THEN 'Normal'
WHEN BP_Systolic BETWEEN '120' AND '139' OR BP_Diastolic BETWEEN '80' AND '89' THEN 'Prehypertension'
WHEN BP_Systolic BETWEEN '140' AND '159' OR BP_Diastolic BETWEEN '90' AND '99' THEN 'Stage 1 Hypertension'
WHEN BP_Systolic >= '160' OR BP_Diastolic >= '100' THEN 'Stage 2 Hypertension'
END AS BP_Category
答案 0 :(得分:2)
我认为只需对CASE
子句进行排序就可以了,因为它是按顺序解析的:
CASE
WHEN BP_Systolic >= '160' OR BP_Diastolic >= '100' THEN 'Stage 2 Hypertension'
WHEN BP_Systolic BETWEEN '140' AND '159' OR BP_Diastolic BETWEEN '90' AND '99' THEN 'Stage 1 Hypertension'
WHEN BP_Systolic BETWEEN '120' AND '139' OR BP_Diastolic BETWEEN '80' AND '89' THEN 'Prehypertension'
WHEN BP_Systolic < '120' AND BP_Diastolic < '80' THEN 'Normal'
END AS BP_Category
虚拟小提琴试试:http://sqlfiddle.com/#!3/8ad7f/3
答案 1 :(得分:0)
case
语句按顺序进行评估。因此,本声明优先考虑“正常”(虽然根据其定义,放置无关紧要)。要优先考虑其他事项,请颠倒顺序:
CASE WHEN BP_Systolic >= '160' OR BP_Diastolic >= '100'
THEN 'Stage 2 Hypertension'
WHEN BP_Systolic BETWEEN '140' AND '159' OR
BP_Diastolic BETWEEN '90' AND '99'
THEN 'Stage 1 Hypertension'
WHEN BP_Systolic BETWEEN '120' AND '139' OR
BP_Diastolic BETWEEN '80' AND '89'
THEN 'Prehypertension'
WHEN BP_Systolic < '120' AND BP_Diastolic < '80'
THEN 'Normal'
END AS BP_Category