Oracle table partition and join on date  question

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

Question by:toooki
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.
LVL 76

Accepted Solution

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'));

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


Author Comment

ID: 34907943
Thank you for the help and suggestions. I am testing the code.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Documenting Data flow 4 40
compare between entities in entity framework 8 73
Help on model clause 5 32
dbms_crypto.decrypt   errors out 6 31
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

809 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