
Above is a screen cap of my schema.
The visits table has a list of personal ID numbers and ICD is a list of codes that pertain to a certain person. You can see from the junction table that there’s a many-to-many relationship between Visits and ICDTable. I’m trying to make a query that allows a person to type in two records in ICDTable and only return the Visit ID # if a person has both of them. I tried to amend this from a previous query I wrote but I can’t figure out what’s going on. The query doesn’t ‘fail’ per se in that it allows me to search for some things, but it’s obviously wrong. What am I missing? Gracias.
SELECT Visits.ID, Inf.desc, Sore.desc
FROM tblKentuckyCounties
INNER JOIN
(
ICDTable AS Inf
INNER JOIN
(
(
(
Visits
INNER JOIN ICDTable AS InfVisits
ON Visits.ID=InfVisits.VisitsID
)
INNER JOIN ICDTable AS SoreVisits
ON Visits.ID=SoreVisits.ID
)
INNER JOIN ICDTable AS Sore
ON SoreVisits.ICD_IDFK=Sore.ID
) ON Inf.ID=Visits.ICD_IDFK
)
ON tblKentuckyCounties.ID=Visits.County
WHERE Inf.desc=[enter first term]
AND Sore.desc=[enter second term]
thanks for edit.
Ok, so I found out what I needed to do. Below this text is the working query for anyone interested in how to do this sort of thing
SELECT DISTINCT Visits.KHA_ID, Visits.totalCharges
FROM (Visits INNER JOIN (ICDTable INNER JOIN ICDVisitsJxn ON ICDTable.ICD9ID = ICDVisitsJxn.ICD_IDFK) ON Visits.ID = ICDVisitsJxn.VisitsIDFK) INNER JOIN (ICDTable AS ICDTable_1 INNER JOIN ICDVisitsJxn AS ICDVisitsJxn_1 ON ICDTable_1.ICD9ID = ICDVisitsJxn_1.ICD_IDFK) ON Visits.ID = ICDVisitsJxn_1.VisitsIDFK
WHERE (((ICDTable.Description) Like [enter term]) AND ((ICDTable_1.Description) Like [enter another term]));
You could divide your query into a subquery containing only ICDVisits and ICDtable (1). In a new query you then join Visits with this subquery twice (2).
(1)
(2)
(Query 1 & 2 being the join of ICDVisits and ICDTable).
Forgot to mention it: You don’t have to use the ID field inside ICDVisitsJxn as a primary key, you could just make the fields VisitsIDFK and ICD_IDFK the primary key. This would avoid duplicated entries.
Query 1 & 2 (being the same obviously):

Combined Query: