Solved

Oracle table partition and join on date  question

Posted on 2011-02-15
3
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 77

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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