Search
Write a publication
Pull to refresh
0
0
Send message
Не изящное решение, но всё же
with price(stock_id, prod_id, start_date, kind, price1, cost1, bonus1) as (
values (1,1,to_date('2000-01-01','YYYY-MM-DD'),'R',100.0,32.12,6.49),
       (1,1,'2000-01-02','P', 80.0, 0,   0),
       (1,1,'2000-01-03','P', 70.0, 0,   0),
       (1,1,'2000-01-04','R',110.0,33.48,6.19),
       (1,1,'2000-01-05','P', 90.0, 0,   0),
       (1,1,'2000-01-06','R',120.0,41.22,6.19),
       (1,1,'2000-01-07','P', 80.0, 0,   0),
       (1,1,'2000-01-08','P', 90.0, 0,   0),
       (1,1,'2000-01-09','R', 93.0,36.87,6.49),
       (1,1,'2000-01-10','R', 94.0,36.85,6.99),
       (1,2,'2000-01-01','R',101.0,52.06,9.00),
       (1,2,'2000-01-02','P', 81.0, 0,   0),
       (1,2,'2000-01-03','P', 71.0, 0,   0),
       (1,3,'2000-01-04','R',111.0,64.96,4.50),
       (1,3,'2000-01-05','P', 92.0, 0,   0),
       (1,3,'2000-01-06','R',122.0,66.83,4.60),
       (1,3,'2000-01-07','P', 82.0, 0,   0),
       (1,3,'2000-01-08','P', 92.0, 0,   0))
, price_hist as 
(select stock_id, prod_id, start_date, kind, coalesce(lead(start_date) over (partition by stock_id, prod_id order by start_date), to_date('5999-12-31', 'YYYY-MM-DD')) end_date
from price p
where kind = 'R')

select 
	stock_id, prod_id, start_date, kind, price1, cost1, bonus1, 
	first_value(price1) over(partition by stock_id, prod_id, check_date order by start_date) check_date
from
	(select 
		stock_id, prod_id, p.start_date, p.kind, price1, cost1, bonus1, end_date,
		first_value(end_date) over(partition by stock_id, prod_id, p.start_date order by end_date) check_date
	from 
		price p left join
		price_hist
			using(stock_id, prod_id)
	where end_date > p.start_date) t
where check_date=end_date

Information

Rating
Does not participate
Registered
Activity