create table billings(
emp varchar(29),
bill_date date,
bill_rate int
);
insert into billings values
('Sehwag', '01-JAN-1989', 15) ,
('Dhoni', '01-JAN-1989', 20) ,
('Sachin', '05-Feb-1991', 30),
('Sachin','01-JAN-1990',25);
create table HoursWorked
(emp_name varchar(20),
work_date date,
bill_hrs int) ;
insert into HoursWorked values
('Sachin', '01-JUL-1990',3) ,
('Sachin', '01-AUG-1990', 5),
('Sehwag','01-JUL-1990', 2),
('Sachin','01-JUL-1991', 4);
with cte as(select a.*, b.emp as e,coalesce ( b.bill_date, a.bill_date) bd ,coalesce ( b.bill_rate, 0) as br from billings a
left join
billings b
on a.emp=b.emp
and a.bill_date>b.bill_date
)
select * from cte
/*,cte2 as(select a.* ,b.* from cte a
right join
hoursworked b
on a.emp=b.emp_name
and
a.bd<b.work_date)
select * from cte2
/*
select a.*,b.*,
from billings a
inner join
Hoursworked b
on a.emp=b.emp_name
order by a.bill_date