Solved

Oracle table partition and join on date  question

Posted on 2011-02-15
3
391 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 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 237 total points
ID: 34906232
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
ID: 34907237
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
ID: 34907943
Thank you for the help and suggestions. I am testing the code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to take different types of Oracle backups using RMAN.
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.

932 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

11 Experts available now in Live!

Get 1:1 Help Now