我有一个选择语句,根据存储过程中包含的“程序”,“观察”或“条件”进行过医生访问的患者数量得出计数。我使用'procedure','observation'和'condition'作为可选参数。 'procedure','condition'和'observation'都存储在不同的表中。目标是计算在一段时间内具有“程序”,“观察”或“条件”的患者数量的“分子”值。
看起来如果同一患者有多个'程序',多个'观察'或多个'条件','计数'得出所有这些而不是用于输入存储的特定'分子'值。程序。
Parameter declarations:
@CareSite VARCHAR(1000), /* where visit occurs */
@AgeStart VARCHAR(10), /* starting age patient's age falls in */
@AgeEnd VARCHAR(10), /* ending age patient's age falls in */
@Gender VARCHAR(10), /* 'men' or 'women'
@Procedure_Numerator VARCHAR(1000)=null, /* procedure the visit is for */
@Condition_Numerator VARCHAR(1000)=null, /* condition the visit is for */
@Observation_Numerator VARCHAR(1000)=null /* observation the visit is for */
SELECT statement:
DECLARE @D1NumVal INT
SET @D1NumVal = (SELECT COUNT(*) AS Numerator
FROM SAS2SQL_DenominatorPersonTest DPT
JOIN SAS2SQL_DenominatorProcedureTest DPRT
ON DPRT.PersonID = DPT.PersonID
JOIN SAS2SQL_DenominatorConditionTest DCT
ON DCT.Person_ID = DPT.PersonID
JOIN SAS2SQL_DenominatorObservationsTest DOT
ON DOT.PersonID = DPT.PersonID
WHERE DPT.D1 = 1
AND DPT.Age >= @AgeStart AND DPT.Age <= @AgeEnd
AND (@Procedure_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DPRT.ProcudureSourceValue =
@Procedure_Numerator)
OR (@Condition_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DCT.X_Condition_Source_Desc =
@Condition_Numerator)
OR (@Observation_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DOT.ObservationSourceValue =
@Observation_Numerator))
我尝试使用'CASE WHEN'方法,但我上面的内容返回的结果更接近我想要生成的内容。
答案 0 :(得分:0)
您可能需要添加括号,因为AND
在OR
之前,第二AND
之后和最后一个之后进行评估。
这似乎符合您的描述:
DECLARE @D1NumVal INT
SET @D1NumVal = (SELECT COUNT(*) AS Numerator
FROM SAS2SQL_DenominatorPersonTest DPT
JOIN SAS2SQL_DenominatorProcedureTest DPRT
ON DPRT.PersonID = DPT.PersonID
JOIN SAS2SQL_DenominatorConditionTest DCT
ON DCT.Person_ID = DPT.PersonID
JOIN SAS2SQL_DenominatorObservationsTest DOT
ON DOT.PersonID = DPT.PersonID
WHERE DPT.D1 = 1
AND DPT.Age >= @AgeStart AND DPT.Age <= @AgeEnd
AND((@Procedure_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DPRT.ProcudureSourceValue =
@Procedure_Numerator)
OR (@Condition_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DCT.X_Condition_Source_Desc =
@Condition_Numerator)
OR (@Observation_Numerator IS NOT NULL AND
DPT.CareSiteName = @CareSite AND DPT.Wave =
@Wave
AND DPT.Gender = @Gender AND
DOT.ObservationSourceValue =
@Observation_Numerator)))