Solved

sql query to fetch previous rows value - 1 in the next column

Posted on 2011-03-07
11
754 Views
Last Modified: 2012-05-11
I have to create a loan account statement in the main report.
In the footnote I need to show the rate of interest changes during the period of account statement as below.

From_date       To_date                       Rate of Interest
1.4.2010          28.5.2010                 16.5%
29.5..2010        9.9.2010                 15.75
10.9.2010                                        16.,25


In the above report  'From_date' and 'ROI'   are fetched from the table.
Please help to fetch to_date with value=  next(from_date) -1
0
Comment
Question by:lkrishna
11 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Are those dates stored as 'dates' or strings?

I'm also not sure what you mean by 'next(from_date)-1'

To add or subtract days from Oracle dates, just do it:

select to_date('1.4.2010','MM.DD.YYYY')-1 from dual;

0
 

Author Comment

by:lkrishna
Comment Utility
The table has only from_date and ROI fields, I need to fetch the to_date

For e.g. in the below report, to_date in the first row is equal to from_date of second row - 1

29.5.2010 -1,

I tried with self join, connect by prior...but yet to get a solution

From_date       To_date                       Rate of Interest
1.4.2010          28.5.2010                 16.5%
29.5..2010        9.9.2010                 15.75
10.9.2010                                        16.,25
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 167 total points
Comment Utility
See if this helps.

It gets easier to read if from_date is actually a date and not a varchar2 column.
drop table tab1 purge;
create table tab1 (from_date varchar2(20), ROI number);

insert into tab1 values('1.4.2010',16.5);
insert into tab1 values('29.5.2010',15.75);
insert into tab1 values('10.9.2010',16.25);
commit;

select from_date, to_char(
	to_date(
		lead(from_date) over(order by to_date(from_date,'DD.MM.YYYY'))
	,'DD.MM.YYYY')-1,'DD.MM.YYYY'),
	roi
from tab1;

Open in new window

0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
Comment Utility
Not sure what you want as output, but see attached as general guideline.
query.txt
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Isn't that pretty much what I posted?

>>The table has only from_date and ROI fields, I need to fetch the to_date

to_date isn't a real column.  It is derived as 1 - the 'next' from date.
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 166 total points
Comment Utility
In CR, you can use Next ({table.From_date}) to get the value from the next record, but you may need to add some restrictions to that.  For example, if the report is reading sets of records that are grouped/sorted by account, on the last record for one account, Next will give you the first From_date for the next account, which is presumably not what you'd want.  Then the question would be, what should "to date" be when there is no next record for an account?

 James
0
 

Author Comment

by:lkrishna
Comment Utility
All the comments are useful and I was able to get the results successfully
0
 

Author Comment

by:lkrishna
Comment Utility
all the expert comments are useful and I was able toget the required results.  Regret for not having responded ,  due to work pressure in the office   ,immediately could not respond.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to recover a database from a user managed backup

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now