Avatar of jvera524
jvera524

asked on 

Aging Formula in update query

Hello there is a date and agingcategory column in one table and an aging lookup in another table.  Every month the aging category needs to be updated using last day of the month.  So if I am updating the information on June 1, 2012 it should use May 31, 2012. If May 1 it should use April 30, 2012.  I need it to automatically calculate the difference between the month end date and the date in the particular field of the date column.  After calculating the number of days get the appropriate aging category for the difference.  There are 13 aging categories.

An example below.
Date used to calculate aging:      May 31, 2012                        June 30, 2012
date column                         Aging column(on June 1)           Aging column(July 1)
May 15, 2012                           0-30 Days                                31-60 Days

Please let me know if any more information is needed.

Thank you in advance.
Oracle Database

Avatar of undefined
Last Comment
ianmills2002
Avatar of Sean Stuber
Sean Stuber

I'm afraid I don't see the pattern yet.

Please provide more sample data and expected results.

Also,  are you looking for a select statement that gives the final results or are you looking for an update statement(s) that modifies one or more of the tables?
Avatar of jvera524
jvera524

ASKER

Another example below.
Date used to calculate aging:      May 31, 2012                        June 30, 2012
date column                         Aging column(on June 1)           Aging column(July 1)
Mar 15, 2012                           61-90 Days                                91-120 Days
May 15, 2012                           0-30 Days                                31-60 Days

It would calculate    5/31/2012-3/15/2012 @ 76 Days     6/30/2012-3/15/2012 @ 106 days
Then it would up look up 76 and 106 and obtain the appropriate aging category from the lookup table.  I want to update the one column in the same table as the date column. Is there a function which would the month end date correctly.

If calculated updated on June 1 2012 or June 2 2012 It would use 5/31/2012 to calculate # of days to lookup

If calculated updated on July 1 2012 or July 4 2012 It would use 6/3o/2012 to calculate # of days to lookup.
Avatar of ianmills2002
ianmills2002
Flag of Australia image

I will assume some of the table structure in this suggested solution...

update table1
set agingcategory = (select agingcategory
                                from agingcategory_table
                                where (trunc(sysdate, 'mm') - 1) - table1.date 
                                           between agingcategory_table.from_days
                                                   and agingcategory_table.to_days
                                )

Open in new window


If I have guessed wrong, we will need more details about the table structures and sample data.
Avatar of ianmills2002
ianmills2002
Flag of Australia image

I was too slow to do my post.

(trunc(sysdate, 'mm') - 1)   equates to the last day of last month

If you have to update 2 columns, then use my subselect for each of the columns
Avatar of jvera524
jvera524

ASKER

Thank you.  I understand your response but have 1 question.
 
Do I actually type
.from_days
.to_days
after agingcategory_table?  Will that look up the difference in the NumDays field and return the corresponding category?

Just in case some more information below

The agingcategory_table has two columns the lookup table

NumDays    Category
1                    0-30 Days
2                    0-30 Days
...
...
...
45                 31-60 Days

The other table table1 has 2 columns(returns the value from the lookup table)
Date               AgeCategory
4/21/2012       0-30 Days
1/12/2012       31-60 Days

Thank you again for your help!
ASKER CERTIFIED SOLUTION
Avatar of ianmills2002
ianmills2002
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Oracle Database
Oracle Database

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo