Oracle SQL - 从最新的三个 GRN 日期(Order By)获取项目的最高 (Max) 价格
Oracle SQL - Get maximum (Max) price of Item from latest three GRN dates (Order By)
在这种情况下我有两个 table,grn_item
table 和 grn
table,我只使用下面的查询获取以下列。
select gi.item_no, g.grn_date, gi.price from grn_item gi
join grn g on gi.grn_no = g.grn_no
示例数据:
ITEM NO | GRN Date | Price
----------------------------------
IT00001 | 03-AUG-21 | 1.2
IT00001 | 01-JUN-21 | 5
IT00001 | 08-MAY-21 | 6
IT00001 | 19-MAR-21 | 7.2
IT00002 | 14-NOV-21 | 3.4
IT00002 | 08-OCT-21 | 1.7
IT00002 | 25-FEB-21 | 2.5
IT00003 | 08-MAY-21 | 4.3
IT00003 | 03-SEP-21 | 8.2
IT00003 | 15-JUL-21 | 0.4
IT00003 | 11-DEC-21 | 9.3
IT00003 | 22-SEP-21 | 10.2
我的要求比较棘手,不知道能不能实现,就是从中得到每个item_no
的Max price最近三个grn_date
的.
示例:- 以下是 IT00003 的最新 grn_date
s 的记录。
其中,最高价格为 10.2
IT00003 | 03-SEP-21 | 8.2
IT00003 | 11-DEC-21 | 9.3
IT00003 | 22-SEP-21 | 10.2
所以我的预期结果应该如下所示。 grn_date
不需要显示,但我将其包含在下面以与上面的示例数据进行比较。
ITEM NO | GRN Date | max(Price)
----------------------------------
IT00001 | 08-MAY-21 | 6
IT00002 | 14-NOV-21 | 3.4
IT00003 | 22-SEP-21 | 10.2
我正在使用 Oracle DB,所以我使用类似这样的东西来获得预期的效果。但是没有成功,可以这样做吗?
select * from (select * from (select gi.item_no, max(gi.price)
from grn_item gi join grn g on gi.grn_no = g.grn_no
order by g.grn_date desc) where rownum <= 3) group by gi.item_no
从您现有的(中间)结果开始,您可以按如下方式完成查询:
更改会话集 nls_date_format = 'dd-MON-rr';
with
intermediate_result (item_no, grn_date, price) as (
select 'IT00001', to_date('03-AUG-21'), 1.2 from dual union all
select 'IT00001', to_date('01-JUN-21'), 5 from dual union all
select 'IT00001', to_date('08-MAY-21'), 6 from dual union all
select 'IT00001', to_date('19-MAR-21'), 7.2 from dual union all
select 'IT00002', to_date('14-NOV-21'), 3.4 from dual union all
select 'IT00002', to_date('08-OCT-21'), 1.7 from dual union all
select 'IT00002', to_date('25-FEB-21'), 2.5 from dual union all
select 'IT00003', to_date('08-MAY-21'), 4.3 from dual union all
select 'IT00003', to_date('03-SEP-21'), 8.2 from dual union all
select 'IT00003', to_date('15-JUL-21'), 0.4 from dual union all
select 'IT00003', to_date('11-DEC-21'), 9.3 from dual union all
select 'IT00003', to_date('22-SEP-21'), 10.2 from dual
)
select item_no, max(price) as max_price_of_last_3
from (
select item_no, price,
row_number() over (partition by item_no
order by grn_date desc) as rn
from intermediate_result
)
where rn <= 3
group by item_no
;
ITEM_NO MAX_PRICE_OF_LAST_3
------- -------------------
IT00001 6
IT00002 3.4
IT00003 10.2
这展示了这个想法,并在您的“输入”数据上对其进行了测试。
最好将其与您现有的查询结合起来,以节省一些工作。未经测试,但我认为它应该是这样的:
select item_no, max(price) as max_price_of_last_3
from (
select gi.item_no, gi.price,
row_number() over (partition by gi.item_no
order by g.grn_date desc) as rn
from grn_item gi join grn g on gi.grn_no = g.grn_no
)
where rn <= 3
group by item_no
;
在这种情况下我有两个 table,grn_item
table 和 grn
table,我只使用下面的查询获取以下列。
select gi.item_no, g.grn_date, gi.price from grn_item gi
join grn g on gi.grn_no = g.grn_no
示例数据:
ITEM NO | GRN Date | Price
----------------------------------
IT00001 | 03-AUG-21 | 1.2
IT00001 | 01-JUN-21 | 5
IT00001 | 08-MAY-21 | 6
IT00001 | 19-MAR-21 | 7.2
IT00002 | 14-NOV-21 | 3.4
IT00002 | 08-OCT-21 | 1.7
IT00002 | 25-FEB-21 | 2.5
IT00003 | 08-MAY-21 | 4.3
IT00003 | 03-SEP-21 | 8.2
IT00003 | 15-JUL-21 | 0.4
IT00003 | 11-DEC-21 | 9.3
IT00003 | 22-SEP-21 | 10.2
我的要求比较棘手,不知道能不能实现,就是从中得到每个item_no
的Max price最近三个grn_date
的.
示例:- 以下是 IT00003 的最新 grn_date
s 的记录。
其中,最高价格为 10.2
IT00003 | 03-SEP-21 | 8.2
IT00003 | 11-DEC-21 | 9.3
IT00003 | 22-SEP-21 | 10.2
所以我的预期结果应该如下所示。 grn_date
不需要显示,但我将其包含在下面以与上面的示例数据进行比较。
ITEM NO | GRN Date | max(Price)
----------------------------------
IT00001 | 08-MAY-21 | 6
IT00002 | 14-NOV-21 | 3.4
IT00003 | 22-SEP-21 | 10.2
我正在使用 Oracle DB,所以我使用类似这样的东西来获得预期的效果。但是没有成功,可以这样做吗?
select * from (select * from (select gi.item_no, max(gi.price)
from grn_item gi join grn g on gi.grn_no = g.grn_no
order by g.grn_date desc) where rownum <= 3) group by gi.item_no
从您现有的(中间)结果开始,您可以按如下方式完成查询:
更改会话集 nls_date_format = 'dd-MON-rr';
with
intermediate_result (item_no, grn_date, price) as (
select 'IT00001', to_date('03-AUG-21'), 1.2 from dual union all
select 'IT00001', to_date('01-JUN-21'), 5 from dual union all
select 'IT00001', to_date('08-MAY-21'), 6 from dual union all
select 'IT00001', to_date('19-MAR-21'), 7.2 from dual union all
select 'IT00002', to_date('14-NOV-21'), 3.4 from dual union all
select 'IT00002', to_date('08-OCT-21'), 1.7 from dual union all
select 'IT00002', to_date('25-FEB-21'), 2.5 from dual union all
select 'IT00003', to_date('08-MAY-21'), 4.3 from dual union all
select 'IT00003', to_date('03-SEP-21'), 8.2 from dual union all
select 'IT00003', to_date('15-JUL-21'), 0.4 from dual union all
select 'IT00003', to_date('11-DEC-21'), 9.3 from dual union all
select 'IT00003', to_date('22-SEP-21'), 10.2 from dual
)
select item_no, max(price) as max_price_of_last_3
from (
select item_no, price,
row_number() over (partition by item_no
order by grn_date desc) as rn
from intermediate_result
)
where rn <= 3
group by item_no
;
ITEM_NO MAX_PRICE_OF_LAST_3
------- -------------------
IT00001 6
IT00002 3.4
IT00003 10.2
这展示了这个想法,并在您的“输入”数据上对其进行了测试。
最好将其与您现有的查询结合起来,以节省一些工作。未经测试,但我认为它应该是这样的:
select item_no, max(price) as max_price_of_last_3
from (
select gi.item_no, gi.price,
row_number() over (partition by gi.item_no
order by g.grn_date desc) as rn
from grn_item gi join grn g on gi.grn_no = g.grn_no
)
where rn <= 3
group by item_no
;