Question about joining SQL

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);
+3
3

Google Analytics ( Oracle) . .

select ndc,
       max(decode(rn, 1, rx_num, null)) rx1,
       max(decode(rn, 2, rx_num, null)) rx2,
       max(decode(rn, 3, rx_num, null)) rx3,
       max(decode(rn, 4, rx_num, null)) rx4
  from (select *
          from (select claims_list.ndc,
                       claims_list.rx_num,
                       row_number() over (partition by claims_list.ndc order by claims_list.date desc) rn
                  from claims_list,
                       (select * 
                          from (select *
                                  from drug_list
                                 where type = 'Generic'
                                order by qty desc
                               )
                         where rownum < 51
                       ) drug_list
                 where drug_list.ndc = claims_list.ndc
               )
         where rn < 5
        order by ndc, rn
       )
group by ndc;

, 4 rx . , 4 4 .

+1

, Oracle... :

SELECT
    "ndc" as NDC,
    ( SELECT "rx_num" from "rx" WHERE "ndc"="drug_list"."ndc" ORDER BY "date" DESC LIMIT 1 OFFSET 0 ) as RX1,
    ( SELECT "rx_num" from "rx" WHERE "ndc"="drug_list"."ndc" ORDER BY "date" DESC LIMIT 1 OFFSET 1 ) as RX2,
    ( SELECT "rx_num" from "rx" WHERE "ndc"="drug_list"."ndc" ORDER BY "date" DESC LIMIT 1 OFFSET 2 ) as RX3,
    ( SELECT "rx_num" from "rx" WHERE "ndc"="drug_list"."ndc" ORDER BY "date" DESC LIMIT 1 OFFSET 3 ) as RX4
FROM "drug_list"
ORDER BY qty ASC
LIMIT 4

NDC | RX1 | RX2 | RX3 | RX4
123   2332  2342  2346  7776
0

This will lead you to every single line:

SELECT
  `cl`.`ndc`,
  `cl`.`rx_num`
FROM `claims_list` AS `cl`
WHERE `cl`.`ndc` IN
  (
    SELECT `dl`.`ndc` FROM `drug_list` AS `dl`
    WHERE `dl`.`type` = 'Generic'
    ORDER BY `dl`.`qty` DESC
    LIMIT 50
  )
ORDER BY `cl`.`date` DESC
LIMIT 4

Then run the results through a filter in the calling script to group them together.

0
source

Source: https://habr.com/ru/post/1721533/


All Articles