How can I do this in MSS-Access sql?

Posted on 2011-10-31
Last Modified: 2013-11-05
I have 2 table with a common key. tblPatient and tblMeds linked by chart. Is there a way in sql to get all the patients that have (for example) MedicationA, MedicationB and MedicationC? I want all the patients that take all 3 medications. Sounds simple but it dosn't seem to be. Thanks in advance.
Question by:DevLSS
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Then why not post what you tried, so we can look at it...?
    LVL 44

    Accepted Solution

    sure.  with the query wizard, join the tblPatient and tblMeds tables.

    Add the patient identifying field to the grid.
    Add the med identifying field to the grid twice.
    Click the summation toolbar button, converting this to a Group By query.
    Change the first Med column type to Count.
    Change the second Med column type to Where
    In the criteria for the second Med column, type an In ( , , ) condition that will select all three meds you are interested in.

    Test this query to make sure you've got the In() clause typed correctly.

    Go back to design mode.
    In the criteria area for the first Med column, type 3.

    Run the query again.
    LVL 44

    Expert Comment

    You have tblPatients and tblMeds.  You need a third table tblPatsMeds containing the Meds taken by each patient.  In this case, you want to know which patients in tblPatsMeds has a record for each of meds MedA, MedB, and MedC.

    TRANSFORM First(PatID)
    FROM tblPatsMeds
    PIVOT MedID IN ("MedA","MedB","MedC")

    Once you see that this works, you can get additional details like FName, LName, MedName by Joining to tblPatients and tblMeds.

    Author Closing Comment

    Thanks, that does it!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now