Here is my situation:
I have one table that contains a list of drugs sold containing NDC (identifier), the number of goods sold, and whether this drug is branded or generic. I have another table that contains the numbers of recipes, dates and NDC.
I need to create a list of the last 4 prescription numbers for the 50 best generic drugs and 50 brand name drugs.
A simplified example:
Drug_list:
NDC QTY Type
123 50 Generic
125 47 Brand
128 34 Generic
...
549 1 Brand
294 1 Generic
Claims_list:
NDC RX_num Date
123 1234 20081027
123 4194 20090517
594 12598 20091012
How to write an entry to create a list
NDC RX1, RX2, RX3, RX4
where are the NDC's 50 most common NDC Brands, and the next RXs are the RX numbers of the most recent applications?
~~~~~~~
So far I have this:
select t.ndc, cl.rx, cl.date from (
select * from (
select * from (
select * from drug_list where brand = 'Generic')
order by qty)
where rownum < 51) t
join claims_list cl on cl.ndc = t.ndc
order by t.ndc, cl.date;
Which gives me part of the way. From there, how do I trim it to 4 results on NDC? And is it possible to get the following:
NDC, RX1, RX2, RX3, RX4
If I need to report this as:
NDC1, RX1
NDC1, RX2
NDC1, RX3
NDC1, RX4
NDC2, RX1
NDC2, RX2
NDC2, RX3
NDC2, RX4
NDC3, RX1
... etc
.
~~~~
( : , , ):
create table drug_list
(NDC varchar2(15), QTY number, type varchar2(10));
create table claims_list
(NDC varchar2(15), RX_num varchar2(20), "date" date);