I have 1 table that used PATid,DISPENESED date,Vamount as well as other fields. The ids can duplicate. I want to be able to find the greatest date per id and then show the amount associated with the date. I then have a second table that has id, dob,enroll date. I want to do a join between the two tables.
Via some experimenting and research I was able to get the first part to occur but when I do the join the amount from table one acquires a value from another date under the id.
the below is the code I created. As stated without the join the results are what I want after the join they are incorrect.
SELECT
MAX(M.HISTORY_CLIENT_DISPENSE. dispense_date),
M.HISTORY_CLIENT_DISPENSE.PATID,
M.HISTORY_CLIENT_DISPENSE.v_dispensed_amount,
M.HISTORY_CLIENT_DISPENSE. V_medication_type_code,
M.HISTORY_CLIENT_DISPENSE. V_medication_type_value,
M.HISTORY_CLIENT_DISPENSE. medication_date,
M.HISTORY_CLIENT_DISPENSE.link_to_rti_CLIENT,
M.HISTORY_CLIENT_DISPENSE.LINK_TO_SPILL_CLIENT,
M.view_epis_sum_cur_AM.PATID
FROM
M.HISTORY_CLIENT_DISPENSE JOIN
M.view_epis_sum_cur_AM
ON (M.HISTORY_CLIENT_DISPENSE.PATID=M.view_epis_sum_cur_AM.PATID)
WHERE
M.hiSTORY_CLIENT_DISPENSE.link_to_rti_CLIENT IS NULL AND
M.HISTORY_CLIENT_DISPENSE.LINK_TO_SPILL_CLIENT IS NULL
GROUP BY
M.HISTORY_CLIENT_DISPENSE.PATID
ORDER BY
M.HISTORY_CLIENT_DISPENSE.PATID,
M.HISTORY_CLIENT_DISPENSE. medication_date
Via some experimenting and research I was able to get the first part to occur but when I do the join the amount from table one acquires a value from another date under the id.
the below is the code I created. As stated without the join the results are what I want after the join they are incorrect.
SELECT
MAX(M.HISTORY_CLIENT_DISPENSE. dispense_date),
M.HISTORY_CLIENT_DISPENSE.PATID,
M.HISTORY_CLIENT_DISPENSE.v_dispensed_amount,
M.HISTORY_CLIENT_DISPENSE. V_medication_type_code,
M.HISTORY_CLIENT_DISPENSE. V_medication_type_value,
M.HISTORY_CLIENT_DISPENSE. medication_date,
M.HISTORY_CLIENT_DISPENSE.link_to_rti_CLIENT,
M.HISTORY_CLIENT_DISPENSE.LINK_TO_SPILL_CLIENT,
M.view_epis_sum_cur_AM.PATID
FROM
M.HISTORY_CLIENT_DISPENSE JOIN
M.view_epis_sum_cur_AM
ON (M.HISTORY_CLIENT_DISPENSE.PATID=M.view_epis_sum_cur_AM.PATID)
WHERE
M.hiSTORY_CLIENT_DISPENSE.link_to_rti_CLIENT IS NULL AND
M.HISTORY_CLIENT_DISPENSE.LINK_TO_SPILL_CLIENT IS NULL
GROUP BY
M.HISTORY_CLIENT_DISPENSE.PATID
ORDER BY
M.HISTORY_CLIENT_DISPENSE.PATID,
M.HISTORY_CLIENT_DISPENSE. medication_date