Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Dividing into date buckets

711126Aug 21 2009 — edited Aug 28 2009
I am trying to divide the following data into date buckets computed from the last date. The intent is to find out what was the quantity outstanding at the end of a given period.

create table products_quality(
prod_id as varchar2(10),
qty_outstanding number(5),
qty_date date
);


insert into products_quality values(912649,300,to_date('8/12/2003','mm/dd/yyyy'));
insert into products_quality values(912649,600,to_date('10/20/2005','mm/dd/yyyy'));
insert into products_quality values(912649,450,to_date('11/24/2006','mm/dd/yyyy'));
insert into products_quality values(912649,700,to_date('1/28/2008','mm/dd/yyyy'));
insert into products_quality values(912649,650,to_date('2/17/2008','mm/dd/yyyy'));
insert into products_quality values(912649,700,to_date('4/7/2008','mm/dd/yyyy'));
insert into products_quality values(912649,800,to_date('7/16/2008','mm/dd/yyyy'));
insert into products_quality values(912649,900,to_date('7/31/2008','mm/dd/yyyy'));
insert into products_quality values(912649,950,to_date('12/28/2008','mm/dd/yyyy'));
insert into products_quality values(912649,1000,to_date('1/1/2009','mm/dd/yyyy'));
insert into products_quality values(912649,1100,to_date('1/17/2009','mm/dd/yyyy'));
insert into products_quality values(912649,1500,to_date('2/1/2009','mm/dd/yyyy'));

The result will group by prod_id and show the latest qty_outstanding within each of the date buckets. Kindly note, if there is no data within that bucket then the data from next bucket is chosen (e.g in bucket (31-90 days) below. The last date is the base date and is not counted in the bucket

Prod_id | qty_date | 1-30days | 31-90days | 91-180days | 180-365days | 366-730days | 731-1095days | 1096-1825days
912649 | '2/1/2009' | 1100 | 1000 | 900 | 900 | 700 | 700 | 450 | 600 | 300


Can we write a plain SQL to do it?

Thanks
This post has been answered by Solomon Yakobson on Aug 21 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 18 2009
Added on Aug 21 2009
4 comments
997 views