Don't know if this is possible but thought I would throw this out there for some help.
I have the following query that I am having trouble with. My problem is that the way the table (Inventory_Item_Fact) is laid out there are multiple rows for the same drug based on inventory received from our wholesaler and prescriptions filled. I am trying to find the record that has the most current date and the quantity on hand at that time. Finding the most current date is easy, it's finding the quantity associated with that date is what I am having problems with. I figure that by using the drug_key and max(date_key) I could find the associated quantity but can't figure out how to join a subquery on two different fields.
Hope this makes sense.
SELECT
TREXONE_DW_DATA.DRUG.DRUG_LABEL_NAME,
TREXONE_DW_DATA.DRUG.DRUG_NDC,
TREXONE_DW_DATA.DRUG.DRUG_PACKAGE_SIZE,
TREXONE_DW_DATA.DRUG.DRUG_DOSAGE_FORM,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.CURRENT_QTY,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.DATE_KEY
FROM
TREXONE_DW_DATA.DRUG
INNER JOIN TREXONE_DW_DATA.INVENTORY_ITEM_FACT ON TREXONE_DW_DATA.INVENTORY_ITEM_FACT.DRUG_KEY = TREXONE_DW_DATA.DRUG.DRUG_KEY
WHERE
TREXONE_DW_DATA.DRUG.DRUG_ROUTE = 'OPHTHALMIC' AND
TREXONE_DW_DATA.DRUG.DRUG_OBSOLETE_DATE IS NULL AND
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.FD_FACILITY_KEY = '510'
GROUP BY
TREXONE_DW_DATA.DRUG.DRUG_LABEL_NAME,
TREXONE_DW_DATA.DRUG.DRUG_NDC,
TREXONE_DW_DATA.DRUG.DRUG_PACKAGE_SIZE,
TREXONE_DW_DATA.DRUG.DRUG_DOSAGE_FORM,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.CURRENT_QTY
[flush]
DBAMJA
It is said that God will give you no more than you can handle. I just wish God didn't have so much faith in me.
I have the following query that I am having trouble with. My problem is that the way the table (Inventory_Item_Fact) is laid out there are multiple rows for the same drug based on inventory received from our wholesaler and prescriptions filled. I am trying to find the record that has the most current date and the quantity on hand at that time. Finding the most current date is easy, it's finding the quantity associated with that date is what I am having problems with. I figure that by using the drug_key and max(date_key) I could find the associated quantity but can't figure out how to join a subquery on two different fields.
Hope this makes sense.
SELECT
TREXONE_DW_DATA.DRUG.DRUG_LABEL_NAME,
TREXONE_DW_DATA.DRUG.DRUG_NDC,
TREXONE_DW_DATA.DRUG.DRUG_PACKAGE_SIZE,
TREXONE_DW_DATA.DRUG.DRUG_DOSAGE_FORM,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.CURRENT_QTY,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.DATE_KEY
FROM
TREXONE_DW_DATA.DRUG
INNER JOIN TREXONE_DW_DATA.INVENTORY_ITEM_FACT ON TREXONE_DW_DATA.INVENTORY_ITEM_FACT.DRUG_KEY = TREXONE_DW_DATA.DRUG.DRUG_KEY
WHERE
TREXONE_DW_DATA.DRUG.DRUG_ROUTE = 'OPHTHALMIC' AND
TREXONE_DW_DATA.DRUG.DRUG_OBSOLETE_DATE IS NULL AND
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.FD_FACILITY_KEY = '510'
GROUP BY
TREXONE_DW_DATA.DRUG.DRUG_LABEL_NAME,
TREXONE_DW_DATA.DRUG.DRUG_NDC,
TREXONE_DW_DATA.DRUG.DRUG_PACKAGE_SIZE,
TREXONE_DW_DATA.DRUG.DRUG_DOSAGE_FORM,
TREXONE_DW_DATA.INVENTORY_ITEM_FACT.CURRENT_QTY
[flush]
DBAMJA
It is said that God will give you no more than you can handle. I just wish God didn't have so much faith in me.