使用从乘法表中的位置和计数

时间:2016-01-07 15:08:40

标签: sql-server

我有两张桌子

问题

列:Patient_NO,Encounter_Date,Diagnosis_1,Diagnosis_2

程序

列:Patient_NO,Procedure_Code

我正在尝试计算问题表中具有特定诊断的唯一客户端的数量,以及过程表中的特定过程。

我在两个地方使用where作为内连接,但是当我运行查询时,计数是一个更大的数字,而不是我只计算具有特定诊断的客户端,我认为这是错误的。

select (select  COUNT(DIAGNOSIS_1) From Problem, Procedure  where problem.PATIENT_NO=procedure.PATIENT_NO and problem.ENCOUNTER_DATE between '10/1/15' and '12/31/15' and problem.DIAGNOSIS_1 IN ( 'Z68.53', 'Z68.54', 'O99.210', 'E66.9', 'E66.3') and Procedure.PROCEDURE_CODE IN ( 'TS141', 'TS140')) 
 + (select  COUNT(Diagnosis_2) From Problem, Procedure  where problem.PATIENT_NO=Procedure.PATIENT_NO and problem.ENCOUNTER_DATE between '10/1/15' and '12/31/15' and problem.DIAGNOSIS_2 IN ( 'Z68.53', 'Z68.54', 'O99.210', 'E66.9', 'E66.3') and Procedure.PROCEDURE_CODE IN ( 'TS141', 'TS140')) 

2 个答案:

答案 0 :(得分:2)

假设您正在尝试计算Patient_no如何满足特定条件,这可能对您有用。

SELECT COUNT(DISTINCT pbm.Patient_NO)
FROM   Problem pbm
    JOIN [Procedure] pdr ON pbm.Patient_NO = pdr.Patient_NO
WHERE  pbm.ENCOUNTER_DATE BETWEEN '10/1/15'
                            AND     '12/31/15'
    AND (pbm.DIAGNOSIS_1 IN ('Z68.53','Z68.54','O99.210','E66.9','E66.3')
            OR pbm.DIAGNOSIS_2 IN ('Z68.53','Z68.54','O99.210','E66.9','E66.3'))
    AND pdr.PROCEDURE_CODE IN ('TS141','TS140')

答案 1 :(得分:0)

如果我理解正确,那么你可能会寻找这个 -

SELECT
    COUNT(pr.Patient_NO)
FROM Problem
INNER JOIN [Procedure] pr
    ON problem.PATIENT_NO = pr.PATIENT_NO
WHERE pr.ENCOUNTER_DATE BETWEEN '10/1/15' AND '12/31/15'
AND (
   pr.DIAGNOSIS_1 IN ('Z68.53', 'Z68.54', 'O99.210', 'E66.9', 'E66.3')
   AND pr.DIAGNOSIS_2 IN ('Z68.53', 'Z68.54', 'O99.210', 'E66.9', 'E66.3')
)
AND pr.PROCEDURE_CODE IN ('TS141', 'TS140')