Home » SQL & PL/SQL » SQL & PL/SQL » Sub Query / Lookup
| Sub Query / Lookup [message #278380] |
Sat, 03 November 2007 10:31  |
tracerrx
Messages: 5 Registered: November 2007 Location: Fort Lauderdale, FL
|
Junior Member |
|
|
Please forgive me in advance, I'm totally new to oracle (fresh from MySQL). I have the following Query for a report which works well:
SELECT
CASE WHEN (RX.STORENBR = '101114') THEN 'CC-FTL'
WHEN (RX.STORENBR = '103073') THEN 'CC-MIA'
WHEN (RX.STORENBR = '102520') THEN 'CC-WPB'
ELSE '0'
END AS PHARMACY_NAME,
CASE WHEN (RX.STORENBR = '101114') THEN '1598762015'
WHEN (RX.STORENBR = '103073') THEN '1063423606'
WHEN (RX.STORENBR = '102520') THEN '1770580292'
ELSE '0'
END AS PHARMACY_NPI,
CASE WHEN (RX.STORENBR = '101114') THEN '1079638'
WHEN (RX.STORENBR = '103073') THEN '1019303'
WHEN (RX.STORENBR = '102520') THEN '1099286'
ELSE '0'
END AS PHARMACY_NCPDP,
CASE WHEN (RX.STORENBR = '101114') THEN 'BN9568861'
WHEN (RX.STORENBR = '103073') THEN 'BN9754450'
WHEN (RX.STORENBR = '102520') THEN 'BA8399962'
ELSE '0'
END AS PHARMACY_DEA,
PATIENT.PATSTOREID AS PT_STORE,
PATIENT.PATKEY AS PT_KEY,
(PATIENT.PATSTOREID || LPAD(PATIENT.PATKEY, 6, 0)) AS PTID,
DOCTOR.NAMELAST AS PRESCRIBER_LAST,
DOCTOR.NAMEFIRST AS PRESCRIBER_FIRST,
DOCTOR.ADRSCITY AS PRESCRIBER_CITY,
DOCTOR.ADRSSTATE AS PRESCRIBER_STATE,
DOCTOR.ADRSZIP AS PRESCRIBER_ZIP,
DOCTOR.STATENUMBER,
DOCTOR.DEAALPHAALPHA || DOCTOR.DEAALPHANBR || DOCTOR.DEAALPHASFX AS PRESCRIBER_DEA,
CASE WHEN (DOCTOR.PLANCODE01 = 'NPI') THEN DOCTOR.PLANID01
WHEN (DOCTOR.PLANCODE02 = 'NPI') THEN DOCTOR.PLANID02
WHEN (DOCTOR.PLANCODE03 = 'NPI') THEN DOCTOR.PLANID03
ELSE '0'
END AS NPI,
DOCTOR.SPECIALTY,
DRUG.NAME, DRUG.DRUGNDCNBR,
PLANS.PLANID,
(NVL(RX.CASHPAID, 0)/100) AS COPAY,
to_char(to_date(RX.FILLDATE, 'yyyyddd'), 'MM/DD/YYYY') AS DISPENSED_DATE,
RX.DISPENSEDQTY01, RX.DAYSUPP,
FROM RX
LEFT OUTER JOIN PLANS ON (RX.PLANSTOREID01=PLANS.PLANCODESTOREID AND RX.PLANKEY01=PLANS.PLANCODEKEY)
LEFT OUTER JOIN DRUG ON (RX.DISPENSEDDRUGSTOREID=DRUG.DRUGNBRSTOREID AND RX.DISPENSEDDRUGKEY=DRUG.DRUGNBRKEY)
LEFT OUTER JOIN DOCTOR ON (RX.DOCNBRSTOREID=DOCTOR.DOCSTOREID AND RX.DOCNBRKEY = DOCTOR.DOCKEY)
LEFT OUTER JOIN PATIENT ON (RX.PATNBRSTOREID=PATIENT.PATSTOREID AND RX.PATNBRKEY=PATIENT.PATKEY)
WHERE RX.FILLDATE >= 2007274
AND RX.FILLDATE <= 2007304
AND DRUG.DRUGNDCNBR = 0004038039
AND NVL(RX.RXSTATUS, 0) <> 16
AND NVL(RX.RXSTATUS, 0) <> 17
AND NVL(RX.RXSTATUS, 0) <> 18
AND NVL(RX.RXSTATUS, 0) <> 20
AND NVL(RX.RXSTATUS, 0) <> 24
AND NVL(RX.RXSTATUS, 0) <> 32
AND NVL(RX.RXSTATUS, 0) <> 33
AND NVL(RX.RXSTATUS, 0) <> 48
However, I wan to add a lookup/subquery for each row returned by the main query to return a new field the LEAST(RX.FILLDATE) on each record returned by the main query. I attempted inserting subquery such as:
(
SELECT * FROM (
SELECT RX.FILLDATE AS FIRST_DATE
FROM RX
LEFT OUTER JOIN DRUG ON (RX.DISPENSEDDRUGSTOREID=DRUG.DRUGNBRSTOREID AND RX.DISPENSEDDRUGKEY=DRUG.DRUGNBRKEY)
WHERE RX.PATNBRSTOREID=PT_STORE
AND RX.PATNBRKEY=PT_KEY
AND DRUG.DRUGNDCNBR = 0004038039
ORDER BY LEAST(RX.FILLDATE)
)
WHERE ROWNUM = 1
)
However, this acts during the query, and not on each row returned from the main query. Any ideas on HOW to return this information for each row of the main query?
Thank You
|
|
|
|
|
|
| Re: Sub Query / Lookup [message #278383 is a reply to message #278382] |
Sat, 03 November 2007 10:44   |
tracerrx
Messages: 5 Registered: November 2007 Location: Fort Lauderdale, FL
|
Junior Member |
|
|
I understand LEAST and MIN. What I don't understand is how to perform and additional query, on the original result set, to return the first date that.
The original query will return something like:
MISC | Record Date
Result 1 | 10/01/2007
Result 2 | 10/02/2007
What I need to also know is the date these patient FIRST had records added:
MISC | This Record Date | Subquery Record Date
Result 1 | 10/01/2007 | 01/01/2006
Result 2 | 10/02/2007 | 02/12/2004
|
|
|
|
|
|
| Re: Sub Query / Lookup [message #278387 is a reply to message #278384] |
Sat, 03 November 2007 11:44   |
tracerrx
Messages: 5 Registered: November 2007 Location: Fort Lauderdale, FL
|
Junior Member |
|
|
I understand that I need to use MIN... and probably something along the lines of:
MIN(RX.FILLDATE) OVER (partition by RX.PATNBRSTOREID, RX.PATNBRKEY) earliest_fill_date
But what I do not understand, is how to execute this on * rather than on the result set of the original "WHERE" clause.
|
|
|
|
|
|
| Re: Sub Query / Lookup [message #278389 is a reply to message #278388] |
Sat, 03 November 2007 12:10   |
tracerrx
Messages: 5 Registered: November 2007 Location: Fort Lauderdale, FL
|
Junior Member |
|
|
Understood, but in the example above how would you only select records with a hire date greater than 1982, and still pull their original hire date which is prior to this, and therefore out of the result set?
I do appreciate the help.
|
|
|
|
|
|
| Re: Sub Query / Lookup [message #278391 is a reply to message #278380] |
Sat, 03 November 2007 12:25   |
tracerrx
Messages: 5 Registered: November 2007 Location: Fort Lauderdale, FL
|
Junior Member |
|
|
Michael,
Thank you for bearing with me. I guess I was not clear in my original question. That is EXACTLY what I needed.
Regards,
|
|
|
|
| Re: Sub Query / Lookup [message #278392 is a reply to message #278391] |
Sat, 03 November 2007 13:02  |
 |
Michel Cadot
Messages: 68770 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
It is important when you post a question to extract the smallest unit that reflects your problem.
Then you 1) better understand your problem and even can solve it this way, 2) let us better understand what the problem is and help you find a solution.
Most of the time, classic scott schema (or another Oracle demo schema) is sufficient to explain the case.
Regards
Michel
|
|
|
|
Goto Forum:
Current Time: Sun Nov 16 15:07:55 CST 2025
|