I am looking to query some data that pertains to medications a patient has been prescribed that are in a certain category. But I also want to show patients that do not have any medications. My query so far:
SELECT
pd.fname,
pd.lname,
pp.drug_name,
pp.drug_strength
FROM
patient_data pd
FULL OUTER JOIN patient_prescr pp on pp.pid = pd.pid
FULL OUTER JOIN formulary f on pp.med_id = f.id
INNER JOIN formulary_categories fc on f.category = fc.id AND fc.id in (34,36,37,38,5)
WHERE
pd.lname = 'Test'
When applying the INNER JOIN
to formulary_categories
, I can correctly specify the category in which the drug I want to specify, but when I do this, it WILL NOT include patients that do not have any medications.
With the INNER JOIN
joining the formulary_categories table, my results look like this:
-----------------------------------------------------------------------
fname | lname | drug_name | drug_strength
-----------------------------------------------------------------------
Cathy Test Clonazepam 0.5mg
Larry Test Librium 25mg
Jennifer Test Vistrail 25mg
-----------------------------------------------------------------------
If I change the INNER JOIN
to a FULL OUTER JOIN
, it simply ignores the category
constraint, and pulls all categories.
However, the query will not include patients that do not have any medications prescribed. Id like my results to look something like:
-----------------------------------------------------------------------
fname | lname | drug_name | drug_strength
-----------------------------------------------------------------------
Cathy Test Clonazepam 0.5mg
Larry Test Librium 25mg
Joe Test NULL NULL
Jennifer Test Vistrail 25mg
Steve Test NULL NULL
-----------------------------------------------------------------------