Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle table partition and join on date  question

Posted on 2011-02-15
3
Medium Priority
?
441 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 948 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 952 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 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