卡在一个问题陈述中,该陈述包括比较两个表的数据
stuck in a problem statement which consist of comparing data for two tables
PFB问题陈述。
我尝试使用 dense_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(价格)。 Action_date 是该商品的 unit_retail 更新为该特定位置的新价格的日期。
一个 item/location 组合可以有多个 action_date,具体取决于价格更新为新价格的天数。
- Table 交易包含日期 (tran_date),商品从特定位置出售给客户的日期(post_date 与 tran_date 相同 - 可以忽略它).
我想知道在特定位置出售给客户的商品的unit_retail(价格)。
补充意见:
1. 对于每个 item/location 组合,我想获取 item/loc 组合在交易期间的零售单位 (tran_date)
2. action_date为unit_retail for item/location随新零售变化的日期。
3. 如果单位零售价在单个 action_date 上多次变化,则取零售价较低的零售价。
您需要一个带有 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
我首先想到的是相关子查询:
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()
与这个问题没有明显的联系。
PFB问题陈述。 我尝试使用 dense_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(价格)。 Action_date 是该商品的 unit_retail 更新为该特定位置的新价格的日期。 一个 item/location 组合可以有多个 action_date,具体取决于价格更新为新价格的天数。
- Table 交易包含日期 (tran_date),商品从特定位置出售给客户的日期(post_date 与 tran_date 相同 - 可以忽略它).
我想知道在特定位置出售给客户的商品的unit_retail(价格)。
补充意见: 1. 对于每个 item/location 组合,我想获取 item/loc 组合在交易期间的零售单位 (tran_date) 2. action_date为unit_retail for item/location随新零售变化的日期。 3. 如果单位零售价在单个 action_date 上多次变化,则取零售价较低的零售价。
您需要一个带有 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
我首先想到的是相关子查询:
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()
与这个问题没有明显的联系。