简体   繁体   中英

Query that returns values where a certain number of rows meet certain condition

I'm trying to make a report and i'm having some dificulty designing a query to show the data i need.

I have 2 tables:

+-----------+------------+----------+
+TherapyID  + CostumerID + ClinicID +
+-----------+------------+----------+
+    1      +  John      + Clinic 1 +
+-----------+------------+----------+
+    2      +  Susan     + Clinic 2 +
+-----------+------------+----------+
+    3      +  Mary      + Clinic 3 +
+-----------+------------+----------+


+-----------+--------------+-----------+--------+
+TherapyID  + TherapyLine  + Treatment + Result +
+-----------+--------------+-----------+--------+
+     1     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     1     +       2      +     B     + Success+
+-----------+--------------+-----------+--------+
+     1     +       3      +     C     + Success+
+-----------+--------------+-----------+--------+
+     2     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     2     +       2      +     B     + Fail   +
+-----------+--------------+-----------+--------+
+     2     +       3      +     C     + Success+
+-----------+--------------+-----------+--------+
+     3     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     3     +       2      +     B     + Success+
+-----------+--------------+-----------+--------+
+     3     +       3      +     C     + Fail   +
+-----------+--------------+-----------+--------+

I need to make a query that shows me only the customers or therapyid's that have successfully received all treatments A,B,C

The Query Result should be like this:

+------------+-------------+----------+---------+-----------+---------+
+ TherapyID  + TherapyLine + Customer +  Clinic + Treatment + Result  +
+------------+-------------+----------+---------+-----------+---------+
+     1      +     1       +  John    + Clinic 1+    A      + Success + 
+------------+-------------+----------+---------+-----------+---------+
+     1      +     2       +  John    + Clinic 1+    B      + Success +
+------------+-------------+----------+---------+-----------+---------+
+     1      +     3       +  John    + Clinic 1+    C      + Success +
+------------+-------------+----------+---------+-----------+---------+

This was the only therapyid where all treatments A,B,C where Success I really don't have any idea on how to query this, what i have tried up to now allways returns results from TherapyID * 2,3 * where result was Success too. Thx in advance for the help.

select t1.TherapyID,TherapyLine,t1.CostumerID as Customer,Clinic,Treatment, 
Result from table1 t1,table2 t2 where t1.TherapyID =t2.TherapyID and
t2.result='Success' group by customerID having count(customeID)=3
SELECT t1.TherapyID,TherapyLine,CustomerID as Customer,ClinicID as Clinic,Treatment,Result  
FROM FROM t1 JOIN t2 ON t1.TherapyID=t2.TherapyID WHERE CustomerID IN 
(SELECT CustomerID
FROM t1 JOIN t2 ON t1.TherapyID=t2.TherapyID AND Result='Success' 
GROUP BY CustomerID 
HAVING COUNT(CustomerID)=3)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM