Solved

Oracle table partition and join on date  question

Posted on 2011-02-15
3
385 Views
Last Modified: 2012-05-11
I have a table like this:
Select * from test3 order by f0,f1,f3;
F0      F1      F2      F3
---------------------------------
A      X      7      6/10/2010
A      X      7      6/11/2010
A      X      17      6/12/2010
A      Y      10      6/11/2010
A      Y      20      6/12/2010
A      Y      20      6/13/2010

Attached the table creation script. This table is created such that the value of F2 is the cumulative (from any row before till the current row) sum of F2 with the partition of (F0, F1). How can I create an output that will take the check the last row in the (F1, F2) partition of the above table and add rows to the O/P with identical last row value but the F3 value added by 1 day in each consecutive entry till sysdate.

The O/P will look like:
A      X      7      6/10/2010
A      X      7      6/11/2010
A      X      17      6/12/2010
A      X      17      6/13/2010
A      X      17      6/14/2010
A      X      17      6/15/2010
A      X      17      6/16/2010
...................--another about 240 records
A      X      17      2/15/2011 -- sysdate
A      Y      10      6/11/2010
A      Y      20      6/12/2010
A      Y      20      6/13/2010
A      Y      20      6/14/2010
A      Y      20      6/15/2010
A      Y      20      6/16/2010
A      Y      20      6/17/2010
.......
A      Y      20      2/15/2011 --sysdate

create table test3(f0 varchar2(100), f1 varchar2(100), f2 number(10), f3 date);
insert into test3 values ('A',  'X', 7, to_date('06/10/2010', 'mm/dd/yyyy'));
insert into test3 values ('A',  'X', 7, to_date('06/12/2010', 'mm/dd/yyyy'));
insert into test3 values ('A',  'X', 17, to_date('06/11/2010', 'mm/dd/yyyy'));
insert into test3 values ('A',  'Y', 10, to_date('06/17/2010', 'mm/dd/yyyy'));
insert into test3 values ('A',  'Y', 20, to_date('06/20/2010', 'mm/dd/yyyy'));
insert into test3 values ('A',  'Y', 20, to_date('06/20/2010', 'mm/dd/yyyy'));

Open in new window

0
Comment
Question by:toooki
3 Comments
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 237 total points
Comment Utility
Your question title is misleading for two reasons, first because you do not have a partitioned table, and second because your query involves only a single table, you don't have a join either.

(Your table design is not normalized, because the value for F2 is derived from other rows.  That can cause other problems for you, but you aren't asking specifically about that.)

It looks like what you are asking for is a query to return more rows that what your table includes.  Simple SQL queries can only return rows that are in a table, not data that doesn't exist.  You will need another table that contains records for each day that you want included in your query results.  Then you will need an "outer join" to this second table to return rows that don't exist in your main table.

Your second table could be something like this:
create table calendar (day date);

Then you need to populate that calendar table with records for each day that you want included in your query output.

I don't have time right now to write the final query for you, but this approach should get you started.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 238 total points
Comment Utility
I don't fully understand the logic.  

For example: A X 17 gets over 200 rows and A Y 10 gets one.

Anyway, below is a test showing how to get all the rows from a given date to sysdate.  Hopefully you can integrate it into your code.


drop table tab1 purge;
create table tab1(col1 char(1), col2 date);

insert into tab1 values('a',to_date('02/10/2011','MM/DD/YYYY'));
insert into tab1 values('b',to_date('02/12/2011','MM/DD/YYYY'));
commit;


select col1, col2+level myDate
from tab1
CONNECT BY LEVEL < sysdate-col2
group by col1, col2+level
order by 1,2
/

Open in new window

0
 

Author Comment

by:toooki
Comment Utility
Thank you for the help and suggestions. I am testing the code.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

771 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

14 Experts available now in Live!

Get 1:1 Help Now