简体   繁体   中英

How to get the values for the next and next next date in a table and outer join with another table

We have a table tbl with columns Alias, Effective_Date, CVal,CPrice, and another table tblA with columns Alias and Other Column

How do we query the table tbl to return values from these 2 rows in 1 row and outer join it with another table tblA (joined on the Alias column):

  1. CVal (NextVal) and CPrice (NextPrice) values from row with Effective_Date the next date after somedate

  2. CVal (SecondVal) and CPrice (SecondPrice) values from row with Effective_Date the next date after the Effective_Date from #1

For ex:

Alias   Effective_Date  CVal   CPrice
A       01-JAN-19       1       100
A       01-JAN-20       2       101
A       01-JAN-21       3       102
A       01-JAN-22       4       103

tblA

Alias   OtherColumn
A       O1
B       O2

Say somedate = '31-DEC-19'

Expected result

(the next date after '31-DEC-19' in the Effective_Date column is 01-JAN-20,

and the next date after that is 01-JAN-21):

Alias   OtherColumn NextVal NextPrice SecondVal SecondPrice
A         O1        2       101       3         102

This below query gives me error "tblA"."ALIAS": invalid identifier

select tblA.*, NextVal, NextPrice, SecondVal, SecondPrice from tblA,
(select t.* from
  (
      lead(CVal, 1) over(order by Effective_Date) as NextVal 
     ,lead(CPrice, 1) over(order by Effective_Date) as NextPrice  
     ,lead(CVal, 2) over(order by Effective_Date) as SecondVal 
     ,lead(CPrice, 2) over(order by Effective_Date) as SecondPrice
     from tbl where Effective_Date >=  '31-DEC-19' and alias = tblA.alias  --> error "tblA"."ALIAS": 
     invalid identifier 
     order by Effective_Date ) t
   where rownum = 1) tblB
 where tblA.alias = tblB.alias(+)

Thank you

So I'm not sure entirely why you have the windowing functions in a subquery. The following seems to get what you're after? ( SQL Fiddle here )

create table tbl (Alias nchar(1), Effective_Date date, CVal int,   CPrice int);

insert into tbl values ('A', '19-JAN-20', 1, 100);
insert into tbl values ('A', '20-JAN-20', 2, 101);
insert into tbl values ('A', '21-JAN-20', 3, 102);
insert into tbl values ('A', '22-JAN-20', 4, 103);
insert into tbl values ('B', '22-JAN-20', 4, 103);

create table tblA (Alias nchar(1), OtherColumn nchar(2));
insert into tblA values ('A', 'O1');
insert into tblA values ('B', 'O2');


with t as (select 
  tbl.Alias, 
  tbl.Effective_Date, 
  tbl.CVal, 
  tbl.CPrice,
  lead(tbl.Cval, 1) OVER (ORDER BY Effective_Date) as NextVal 
  ,lead(CPrice, 1) over(order by Effective_Date) as NextPrice  
  ,lead(CVal, 2) over(order by Effective_Date) as SecondVal 
  ,lead(CPrice, 2) over(order by Effective_Date) as SecondPrice
  ,rank() over (partition by tbl.Alias order by tbl.Effective_Date) r
from tblA, tbl
where tblA.Alias = tbl.Alias )
select *
from t
  where
  /*Effective_Date >=  '31-DEC-19' AND */
  r = 1

Try this:

select t.alias,tblA.othercolumn,t.effective_date,t.nextval,t.nextprice,t.secondval,
t.secondprice from (select 
         Alias,
         effective_date,
          lead(CVal, 1) over(order by Effective_Date) as NextVal 
         ,lead(CPrice, 1) over(order by Effective_Date) as NextPrice  
         ,lead(CVal, 2) over(order by Effective_Date) as SecondVal 
         ,lead(CPrice, 2) over(order by Effective_Date) as SecondPrice
         from tbl A where Effective_Date >=  '31-DEC-19' and exists
         (select * from tblA B where A.alias=B.alias)
    )t join tblA on t.alias=tblA.alias  where rownum<2;

Output

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM