Solved

Oracle Fill date Gap

Posted on 2013-12-04
13
526 Views
Last Modified: 2014-03-30
I need help with the Oracle SQL query, I'm getting the data in this format. Fiscal period are 1-6 ,starting from any month and ending at 6 without gap, Gap between starting and end month.( in this case the missing month needs a new record )

Example data :

Order No      Date      
100      1-Jan-13      
100      2-Jan-13      
100      4-Feb-13      
100      6-Apr-13      
100      8-May-13      
100      10-Jun-13      
200      6-Apr-13      
200      7-May-13      
200      8-Jun-13      
300      1-Jan-13      
300      7-May-13      
300      8-Jun-13      
            
Expected Result             
            
Order No      Date      Period
100      1-Jan-13      1
100      2-Jan-13      1
100      4-Feb-13      2
100      6-Apr-13      4
100      8-May-13      5
100      10-Jun-13      6
200      6-Apr-13      4
200      7-May-13      5
200      8-Jun-13      6
300      1-Jan-13      1
300      1-Feb-13      2
300      1-Mar-13      3
300      1-Apr-13      4
300      7-May-13      5
300      8-Jun-13      6

Thanks
0
Comment
Question by:tech29
[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
  • 3
  • 2
  • +1
13 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39697145
Sorry but I don't understand the question. I think you are asking for:

You have a table with dates, and you wish to calculate "fiscal period"

BUT, it looks like "fiscal period" is the same as the month number
Jan = 1, feb = 2, ... Jun = 6

what happens if the dates are
Jul, Aug, Sep ... Dec ?

Plus:
These are the 3 extra rows
300      1-Feb-13      2
300      1-Mar-13      3
300      1-Apr-13      4

Why is this not required as well?
100 1-Mar-13
0
 
LVL 32

Expert Comment

by:awking00
ID: 39698527
Your question is a little ambiguous. Can you provide a more detailed description of what you're trying to accomplish?
0
 

Author Comment

by:tech29
ID: 39698707
Hi PortletPaul/awking00,

There are three orders i.e. 100,200,300

Order Number 100 : Starting Date 01-Jan-2013 and End Date 10-June-2013. Orders are placed every month so there is no issue.

Order Number 200 : Starting Date = 6-Apr-13 and End Date =8-Jun-13. Orders are placed every month from 6-Apr-13 till End date 8-Jun-13, no issue with data.

Order Number 300 : Starting Date = 1-Jan-13 and End Date 8-Jun-13 . Orders are missed in few months in this range i.e. Feb, March,April. In such case 3 new records to be added with first day of the missing records..i.e.

300    01-Feb-2013
300    01-Mar-2013
300    01-Apr-2013


Thanks
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 74

Expert Comment

by:sdstuber
ID: 39698734
>>> Order Number 100 :  Starting Date 01-Jan-2013 and End Date 10-June-2013. Orders are placed every month so there is no issue.


there is no March order for 100 in your sample data,  I assume that means one should be filled in for March 1, 2013  ?
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39698779
if my assumption is correct then try this...


  SELECT orderno,
         CASE
             WHEN COLUMN_VALUE = 1 THEN orderdate
             ELSE ADD_MONTHS(TRUNC(orderdate, 'mm'), COLUMN_VALUE - 1)
         END
             orderdate,
         TO_NUMBER(TO_CHAR(orderdate, 'mm')) + COLUMN_VALUE - 1 period
    FROM (SELECT orderno,
                 orderdate,
                 TRUNC(orderdate, 'mm'),
                 MONTHS_BETWEEN(
                     LEAD(TRUNC(orderdate, 'mm'), 1) OVER(PARTITION BY orderno ORDER BY orderdate),
                     TRUNC(orderdate, 'mm')
                 )
                     gap
            FROM yourtable),
         TABLE(
                 SELECT COLLECT(LEVEL)
                   FROM DUAL
             CONNECT BY LEVEL <= gap
         )
ORDER BY orderno, orderdate
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39699792
You still haven't answered my questions (one shared with sdstuber now).

Why is March not required for order 100?

and:
Is fiscal period just the month number?

What is supposed to happen if the sample data was 1, 3 or 6 months later?

====
Using the query by sdstuber on your sample data, it produces the following result:
| ORDERNO |                       ORDERDATE | PERIOD |
|---------|---------------------------------|--------|
|     100 |  January, 01 2013 00:00:00+0000 |      1 |
|     100 |  January, 02 2013 00:00:00+0000 |      1 |
|     100 | February, 04 2013 00:00:00+0000 |      2 |
|     100 |    March, 01 2013 00:00:00+0000 |      3 | << This is missing in your sample
|     100 |    April, 06 2013 00:00:00+0000 |      4 |
|     100 |      May, 08 2013 00:00:00+0000 |      5 |
|     100 |     June, 10 2013 00:00:00+0000 |      6 |
|     200 |    April, 06 2013 00:00:00+0000 |      4 |
|     200 |      May, 07 2013 00:00:00+0000 |      5 |
|     200 |     June, 08 2013 00:00:00+0000 |      6 |
|     300 |  January, 01 2013 00:00:00+0000 |      1 |
|     300 | February, 01 2013 00:00:00+0000 |      2 |
|     300 |    March, 01 2013 00:00:00+0000 |      3 |
|     300 |    April, 01 2013 00:00:00+0000 |      4 |
|     300 |      May, 07 2013 00:00:00+0000 |      5 |
|     300 |     June, 08 2013 00:00:00+0000 |      6 |

Open in new window

If 1 month is added to the sample orderdate, using the same query the result would be:
| ORDERNO |                       ORDERDATE | PERIOD |
|---------|---------------------------------|--------|
|     100 | February, 01 2013 00:00:00+0000 |      2 |
|     100 | February, 02 2013 00:00:00+0000 |      2 |
|     100 |    March, 04 2013 00:00:00+0000 |      3 |
|     100 |    April, 01 2013 00:00:00+0000 |      4 |
|     100 |      May, 06 2013 00:00:00+0000 |      5 |
|     100 |     June, 08 2013 00:00:00+0000 |      6 |
|     100 |     July, 10 2013 00:00:00+0000 |      7 |
|     200 |      May, 06 2013 00:00:00+0000 |      5 |
|     200 |     June, 07 2013 00:00:00+0000 |      6 |
|     200 |     July, 08 2013 00:00:00+0000 |      7 |
|     300 | February, 01 2013 00:00:00+0000 |      2 |
|     300 |    March, 01 2013 00:00:00+0000 |      3 |
|     300 |    April, 01 2013 00:00:00+0000 |      4 |
|     300 |      May, 01 2013 00:00:00+0000 |      5 |
|     300 |     June, 07 2013 00:00:00+0000 |      6 |
|     300 |     July, 08 2013 00:00:00+0000 |      7 |

Open in new window

or if 3 months was added to the sample:
| ORDERNO |                        ORDERDATE | PERIOD |
|---------|----------------------------------|--------|
|     100 |     April, 01 2013 00:00:00+0000 |      4 |
|     100 |     April, 02 2013 00:00:00+0000 |      4 |
|     100 |       May, 04 2013 00:00:00+0000 |      5 |
|     100 |      June, 01 2013 00:00:00+0000 |      6 |
|     100 |      July, 06 2013 00:00:00+0000 |      7 |
|     100 |    August, 08 2013 00:00:00+0000 |      8 |
|     100 | September, 10 2013 00:00:00+0000 |      9 |
|     200 |      July, 06 2013 00:00:00+0000 |      7 |
|     200 |    August, 07 2013 00:00:00+0000 |      8 |
|     200 | September, 08 2013 00:00:00+0000 |      9 |
|     300 |     April, 01 2013 00:00:00+0000 |      4 |
|     300 |       May, 01 2013 00:00:00+0000 |      5 |
|     300 |      June, 01 2013 00:00:00+0000 |      6 |
|     300 |      July, 01 2013 00:00:00+0000 |      7 |
|     300 |    August, 07 2013 00:00:00+0000 |      8 |
|     300 | September, 08 2013 00:00:00+0000 |      9 |

Open in new window

Would these results be correct for those adjustments to your sample data?

see: http://sqlfiddle.com/#!4/03d2e/12
0
 

Author Comment

by:tech29
ID: 39699815
Hi PortletPaul,

Sorry I missed March..thanks for your help
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39699831
Oh dear, that's not right. I did not solve the question - all I have done is ask questions.

The only offered solution was by sdstuber.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39965682
http:#a39698779 provided the accepted answer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

635 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