Dblank,
I do have one more question...I should have explained this exactly the way that it is (I am pulling Data from Multiple Tables, not just one Table)...
In your previous reply, you posted that I could try this:
SELECT table1.patientid, table1.visitdate, table1.visitid, table1.doctor
FROM table1 INNER JOIN
(SELECT patientid, COUNT(DISTINCT visitid) AS Visits
FROM table1 AS INCIDENTS_1
WHERE (visitdate BETWEEN @startdate AND @enddate)
GROUP BY patientid
HAVING (COUNT(DISTINCT visitid) > 1)) AS Visits ON table1.patientid = Vistis.patientid
My problem is this:
Since I am pulling from Multiple Tables, I am somewhat unsure of where to put the Nested Query…
I am thinking that, since the Person.PID and Document.DCRID (field that I need to do a count on) are from the Person and Document tables, respectively, I should put the Nested Query between them in the FROM Clause.
Does this sound about right??
This is what I currently have in my Add Command (without the line breaks…just did that for ease of use for this example):
SELECT DISTINCT "USR"."PVID", "PERSON"."PID", "USR"."FIRSTNAME", "USR"."LASTNAME", "PERSON"."LASTNAME", "PERSON"."FIRSTNAME", "DOCUMENT"."DCRID", "DOCUMENT"."DB_CREATE_DATE" , "PERSON"."DATEOFBIRTH", "OBS"."OBSVALUE", "OBS"."OBSDATE", "DOCTYPES"."DESCRIPTION", "OBSHEAD"."NAME", "DOCTYPES"."DTID"
FROM (((("ML"."PERSON" "PERSON" INNER JOIN "ML"."DOCUMENT" "DOCUMENT" ON "PERSON"."PID"="DOCUMENT"."PID") INNER JOIN "ML"."OBS" "OBS" ON "PERSON"."PID"="OBS"."PID") INNER JOIN "ML"."OBSHEAD" "OBSHEAD" ON "OBS"."HDID"="OBSHEAD"."HDID") INNER JOIN "ML"."USR" "USR" ON "DOCUMENT"."USRID"="USR"."PVID") INNER JOIN "ML"."DOCTYPES" "DOCTYPES" ON "DOCUMENT"."DOCTYPE"="DOCTYPES"."DTID"
WHERE "PERSON"."LASTNAME"='TEST' AND "DOCUMENT"."DB_CREATE_DATE">={?Start Date} AND "DOCUMENT"."DB_CREATE_DATE"<={?End Date} AND ("DOCTYPES"."DTID"=1 OR "DOCTYPES"."DTID"=1.51680266801937e+015)
ORDER BY "USR"."PVID", "PERSON"."PID"
I am really confused as to how to incorporate this Nested Query into my Main Query…
I believe that I am also a little confused as to what the "AS Visits" and "AS Incidents" were for...
In addition, since the two fields, in my Subquery, are from 2 separate tables (Person and Document), would I need to do an Inner Join on those tables BEFORE I ever tried to do an Inner Join on the Person (Person.PID) table and the Inner Query??
Thanks,
Ron