Solved

Oracle Fill date Gap

Posted on 2013-12-04
13
512 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 48

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 48

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 48

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

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

726 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