Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Qualifying by subquery on multiple fields

Status
Not open for further replies.

DBAMJA

Programmer
Jul 25, 2003
169
0
0
US
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.
 

We don't see any tables.
We don't see any data.
We don't see ant expected results.
...
We can't help you.
[nosmiley]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top