2017-07-16 114 views
0

我有一个select语句,它根据已经为“程序”,“观察”或者“包含在存储过程中的'条件'。我正在使用'程序','观察'和'条件'作为可选参数。 '程序','条件'和'观察'都存储在不同的表格中。目标是计算一段时间内具有“程序”,“观察”或“条件”的患者数量的“分子”值。Where子句返回Count()的所有行和非特定行()

看起来如果同一个患者有多个'程序',多个'观察'或多个'条件','计数'是派生所有这些,而不是针对特定的“分子”值存储过程。

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何时”的做法,但试了一下我上面已经返回的结果更接近我试图生成。

回答

0

您可能需要添加括号,因为ANDOR之前被评估,在第二个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))) 
+0

谢谢你的建议,dnoeth。它看起来像工作。我现在只看到包含每波适当计数的结果。访问发生在每个wave的开始日期和结束日期之间,大概是3个月的时间。感谢您的帮助。 – M72

+0

嗯......它看起来像在关于计数关闭的其他分子值进一步测试之后仍然存在问题。我今天会更加努力,看看我能否解决它... – M72

相关问题