A表的结构: productid,cbdate,nnum;
B表的结构:productid,ddate,price;
现在要取数B表中日期小于A表日期的最大日期的单价,目前我设计的思路是:
select A.cinvbasid,A.dbilldate,
(select B2.price from B B2 where B2.ddate=(select max(B1.ddate) from B B1
where B1.productid=A.productid and B1.ddate<=A.ddate)
and B2.productid=A.productid)
from A
中间红色部分就是关键的提取B表的单价,但是这个效率太低了,有没有大侠帮忙优化一下,效果高些。 PL/SQL性能优化
B表的结构:productid,ddate,price;
现在要取数B表中日期小于A表日期的最大日期的单价,目前我设计的思路是:
select A.cinvbasid,A.dbilldate,
(select B2.price from B B2 where B2.ddate=(select max(B1.ddate) from B B1
where B1.productid=A.productid and B1.ddate<=A.ddate)
and B2.productid=A.productid)
from A
中间红色部分就是关键的提取B表的单价,但是这个效率太低了,有没有大侠帮忙优化一下,效果高些。 PL/SQL性能优化
from A
left join (select b.*,
row_number() over(partition by b.productid order by b.ddate) rn
from b) b
on b.productid = A.productid
and b.ddate <= A.ddate
and rn = 1