Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Treusser
Starting Member
3 Posts |
Posted - 2011-08-04 : 15:10:18
|
I am new to SQL so this is probably a very basic query that I am trying to run but it has me stumped. I am not even sure I titled the topic correctly for what I am wanting to accomplish.I have multiple tables that I need pulled together to find out which patients have had a certain combination of appointments, namely consults and surgeries.I can pull the information for patients with 1 type of appointment, or all patients with either type, but not exclusively patients that have both. Obviously if I run the WHERE type=1 AND type=2 the results are blankHere is my query so far for pulling patients with either type.select patientst.userdefinedid, persont.last, persont.first,appointmentst.notes, appointmentst.starttime, appointmentpurposet.purposeid from patientstinner join personton persont.id=patientst.personidinner join appointmentston patientst.personid=appointmentst.patientidinner join appointmentpurposeton appointmentst.id = appointmentpurposet.appointmentidwhere appointmentpurposet.purposeid in (394, 355)order by last ascWhat do I need to do so I can see only patients who have had both types of appointments? |
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2011-08-04 : 23:34:34
|
Try this script,select persont.id,COUNT(*) AS CountChk INTO #TEMPPatient from patientstinner join persont on persont.id=patientst.personidinner join appointmentst on patientst.personid=appointmentst.patientidinner join appointmentpurposet on appointmentst.id = appointmentpurposet.appointmentidwhere appointmentpurposet.purposeid in (394, 355)GROUP BY persont.idHAVING COUNT(*) > 1select patientst.userdefinedid, persont.last, persont.first,appointmentst.notes, appointmentst.starttime, appointmentpurposet.purposeid from patientstinner join persont on persont.id=patientst.personidinner join appointmentst on patientst.personid=appointmentst.patientidinner join appointmentpurposet on appointmentst.id = appointmentpurposet.appointmentidINNER JOIN #TEMPPatient ON #TEMPPatient.id=persont.idorder by last ascSQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
Treusser
Starting Member
3 Posts |
Posted - 2011-08-05 : 12:37:59
|
Thanks for your response and script. Every little bit I see and use helps me understand it better.I ran the script and it's still grabbing patients who've had either type of appointment, not just ones that have had both.As a bit of background, we need to see which patients who've had a laser procedure(type 394), had a consult(type 355) beforehand. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-08-05 : 13:32:06
|
Maybe you can do something like this in a HAVING clause to get the results you want:DECLARE @T TABLE (ID INT, Val INT)INSERT @T (ID, Val)VALUES(1, 394),(2, 394),(3, 394),(4, 394),(5, 394),(1, 899),(2, 899),(3, 899),(4, 899),(1, 355),(3, 355),(5, 355)SELECT IDFROM @TGROUP BY IDHAVING SUM(CASE WHEN Val = 394 THEN 1 ELSE 0 END) > 0 AND SUM(CASE WHEN Val = 355 THEN 1 ELSE 0 END) > 0 |
 |
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-08-10 : 15:03:58
|
quote: What do I need to do so I can see only patients who have had both types of appointments?
You don't need to display purposeId because you know you want appointments with both purposes.select pat.userdefinedid, prs.last, prs.first, app.notes, app.starttimefrom patientst patjoin persont prs on prs.id=pat.personidjoin appointmentst app on pat.personid=app.patientidjoin appointmentpurposet srg on app.id = srg.appointmentid and srg.purposeId = 394join appointmentpurposet cns on app.id = cns.appointmentid and srg.purposeId = 355order by last asc MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
|
|
|
|