SQL joining question

后端 未结 3 1888
庸人自扰
庸人自扰 2020-12-21 08:28

Here is my situation:

I have one table that contains a list of drugs sold containing the NDC (an identifier), quantity sold, and whether the drug is a brand name or

3条回答
  •  不思量自难忘°
    2020-12-21 09:07

    Making some assumptions, and I'm not sure the exact Oracle syntax....but what about:

    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
    

提交回复
热议问题