卡在问题陈述中,该陈述包含比较两个表的数据

时间:2018-09-22 11:24:43

标签: sql oracle dense-rank

PFB问题说明。 我尝试了density_rank,保持功能,但以某种方式我无法破解它。谁能帮忙。这是用于表示问题的小数据集。原始表有几百万行。

数据集脚本:

CREATE TABLE TRANSACTION (  
    ITEM VARCHAR2(25 BYTE), 
    LOCATION NUMBER(10,0), 
    TRAN_DATE DATE, 
    POST_DATE DATE 
) 


Insert into TRANSACTION (ITEM,LOCATION,TRAN_DATE,POST_DATE) values ('13252099',473,to_date('09-JUL-18','DD-MON-RR'),to_date('09-JUL-18','DD-MON-RR'));
Insert into TRANSACTION (ITEM,LOCATION,TRAN_DATE,POST_DATE) values ('13252099',473,to_date('25-JUL-18','DD-MON-RR'),to_date('25-JUL-18','DD-MON-RR'));
Insert into TRANSACTION (ITEM,LOCATION,TRAN_DATE,POST_DATE) values ('13252098',470,to_date('09-JUL-18','DD-MON-RR'),to_date('09-JUL-18','DD-MON-RR'));
Insert into TRANSACTION (ITEM,LOCATION,TRAN_DATE,POST_DATE) values ('13252098',470,to_date('28-JUL-18','DD-MON-RR'),to_date('28-JUL-18','DD-MON-RR'));



CREATE TABLE RETAIL_DESC (  
    ITEM VARCHAR2(25 BYTE), 
    LOC NUMBER(10,0), 
    UNIT_RETAIL NUMBER(20,4), 
    ACTION_DATE DATE 
)  


Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252099',473,379.97,to_date('09-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252099',473,299.97,to_date('22-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252099',473,0.01,to_date('19-AUG-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252099',473,379.97,to_date('25-AUG-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252098',470,500.18,to_date('08-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252098',470,299.97,to_date('09-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252098',470,0.01,to_date('19-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252098',470,379.97,to_date('24-JUL-18','DD-MON-RR'));
Insert into RETAIL_DESC (ITEM,LOC,UNIT_RETAIL,ACTION_DATE) values ('13252098',470,300,to_date('24-JUL-18','DD-MON-RR'));

enter image description here

注意:我正在使用oracle 11g数据库。 1.Table Retail_desc包含特定位置项目的unit_retail(price)。 Action_date是该商品的unit_retail更新为该特定位置的新价格的日期。 一个项目/位置组合可以根据价格更新为新价格的天数来指定多个action_date。

  1. 表交易中包含从特定位置向客户出售商品的日期(tran_date)(post_date与tran_date相同-可以忽略它)。

我想知道在特定位置出售给客户的商品的unit_retail(价格)。

其他评论: 1.对于每个项目/位置组合,我要获取交易期间(tran_date)该项目/位置组合具有的单位零售 2. action_date是商品/位置的unit_retail随着新零售发生变化的日期。 3.如果单位零售在单个action_date上发生多次更改,则获取价格较低的零售。

2 个答案:

答案 0 :(得分:0)

您需要一个带有join和row_number的子查询:

select item, loc, unit_retail, tran_date
from (
  select
      r.item, r.loc, r.unit_retail, t.tran_date
    , row_number() over (partition by r.item, r.loc, t.tran_date order by r.action_date desc, r.unit_retail) as rn
  from transaction t
  inner join retail_desc r on
    t.item = r.item
    and t.location = r.loc
  where t.tran_date >= r.action_date
) t
where rn = 1
order by item desc, loc desc, tran_date

输出示例数据:

ITEM        LOC     UNIT_RETAIL  TRAN_DATE
13252099    473     379,97       09.07.2018 00:00:00
13252099    473     299,97       25.07.2018 00:00:00
13252098    470     299,97       09.07.2018 00:00:00
13252098    470     300          28.07.2018 00:00:00

答案 1 :(得分:0)

我首先想到一个相关的子查询:

select t.*,
       (select min(rd.unit_retail) keep (dense_rank first order by rd.acction_date desc)
        from retail_desc rd
        where rd.loc = t.location and rd.item = t.item and
              rd.action_date <= t.post_date
       ) as unit_retail
from transaction t;

但是,最好使用lead()join

select t.*, rd.unit_retail
from transaction t left join
     (select rd.*,
             lead(rd.action_date) over (partition by item, loc order by action_date) as next_action_date
      from retail_desc rd
     ) rd
     on t.location = rd.loc and t.item = rd.item and
        t.post_date >= rd.action_date and
        (t.post_date < rd.next_action_date or rd.next_action_date is null);

dense_rank()与该问题没有明显联系。