Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-07
11
Medium Priority
?
799 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
8 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35059057
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
ID: 35059200
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 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 668 total points
ID: 35059252
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Assisted Solution

by:awking00
awking00 earned 668 total points
ID: 35060353
Not sure what you want as output, but see attached as general guideline.
query.txt
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35060384
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 35

Assisted Solution

by:James0628
James0628 earned 664 total points
ID: 35067914
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
ID: 35308160
All the comments are useful and I was able to get the results successfully
0
 

Author Comment

by:lkrishna
ID: 35308167
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

782 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