SQL满足条件a且满足条件b无结果

时间:2015-07-21 13:15:27

标签: sql

这有点具体,所以请耐心等待......

我希望我的where语句能够提供符合以下条件的所有结果:

WHERE
TestCode = A1 
AND
TestResult > 50
AND
TestCode = A2
AND
TestResult > 200

实际上,我想要一份既有A1>50又有A2 > 200

的患者的清单

我可以看到这不会起作用"按照"因为我给了这两个更大的子句,但是我添加括号来表示我想要的东西,它不会返回任何数据。

原始(不工作)查询

      AND 
(SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0165'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '35')
  AND  
(SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0155'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '25')
)

工作代码

      AND 
((SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0165'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '35') OR (SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0155'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '25'))
  AND  
((SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0155'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '25') OR (SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR  =  'A0165'  AND  SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '35'))

抱歉格式化...

8 个答案:

答案 0 :(得分:2)

我认为您之后的语法是:

SELECT * 
FROM    your_table AS A
WHERE   EXISTS (
            SELECT TOP 1 1 
            FROM    your_table AS A1_50
            WHERE   A1_50.ID = A.ID
                AND A1_50.TestCode = 'A1'
                AND A1_50.TestResult > 50)
    AND EXISTS (
            SELECT TOP 1 1 
            FROM    your_table AS A2_200
            WHERE   A2_200.ID = A.ID
                AND A2_200.TestCode = 'A2'
                AND A2_200.TestResult > 200)

只需将your_table替换为您要查询的表格,并使用您要检查以获得分数>的patient_id列。 50/200

编辑: 您也可以使用INTERSECT:

SELECT  PatientID 
FROM    your_table 
WHERE   TestCode = 'A1'
    AND TestResult > 50
INTERSECT 
SELECT  PatientID 
FROM    your_table 
WHERE   TestCode = 'A2'
    AND TestResult > 200

答案 1 :(得分:1)

我认为你想要这样的东西(猜测表和字段名称):

SELECT * from Patients p1
WHERE EXISTS (SELECT NULL FROM Patients p2 
              WHERE p1.ID = p2.ID
                 AND p2.TestCode = A1 AND p2.TestResult > 50)  
  AND EXISTS (SELECT NULL FROM Patients p2 
              WHERE p1.ID = p2.ID
              AND p2.TestCode = A2 AND p2.TestResult > 200)

答案 2 :(得分:1)

这样的东西应该适用于几乎每个rdbms:

SELECT *
FROM YourTable t1
WHERE TestCode = 'A1' AND TestResult > 50
AND EXISTS(
   SELECT 1
   FROM YourTable t2 
   WHERE t2.patientId = t1.PatientId
   AND TestCode = 'A2' AND TestResult > 200
)

答案 3 :(得分:1)

问题是没有行都有TestCode = A1和TestCode = A2。您可以将条件更改为

WHERE (TestCode = A1 AND TestResult > 50) OR (TestCode = A2 AND TestResult > 200)

或像这样使用UNION:

SELECT * FROM foo WHERE TestCode = 'A1' AND TestResult > 50 UNION SELECT * FROM foo WHERE TestCode = 'A2' AND TestResult > 50

答案 4 :(得分:1)

我想你的表看起来像这样:

TestCode    TestResult   PersonId
A1           30           1
A1           60           2
A1           90           3
A2           60           3
A2           90           1
A2          120           2

然后你可以这样做:

SELECT t1.PersonId 
FROM TestData t1
INNER JOIN TestData t2 on t1.PersonId = t2.PersonId
WHERE t1.TestCode = 'A1' AND t2.TestCode = 'A2'
AND t1.TestResult > 50
AND t2.TestResult > 100;

答案 5 :(得分:0)

您需要joinTestCode两次的表格,一次用于A1测试,一次用于A2测试。然后你的where条款可以是:

WHERE a1.TestResult > 50 AND a2.TestResult > 200

答案 6 :(得分:0)

如果您希望患者通过两个测试,那么我建议group by使用having条款:

select PatientId
from . ..
group by PatientId
having sum(case when SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR = 'A0165'  AND  
                     SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '35'
                then 1 else 0 end) > 0 and
       sum(case when SQLUser.EP_VisitTestSetData.VISTD_TestCode_DR = 'A0155'  AND
                      SQLUser.EP_VisitTestSetData.VISTD_TestData  >  '25'
                then 1 else 0 end) > 0;

sum()子句中的每个having计算条件为真的记录数。 > 0确保至少有一条此类记录。

我喜欢这种方法,因为它很灵活 - 你可以轻松地添加另一个测试,或测试三个测试中的两个。

答案 7 :(得分:-1)

你想做这样的事吗?

 WHERE ((TestCode = A1) AND (TestResult > 50)) OR ((TestCode = A2) AND (TestResult > 200))

看起来您正在测试WHEN TestCode = 'A1' AND WHEN TestCode = 'A2'这对他们来说是不可能同时等于两者,返回为空。