Solved

Calculate number of days between months avoiding oracle analytic functions

Posted on 2014-03-21
11
759 Views
Last Modified: 2014-03-27
I will like to know how  can I calculate the differences of two dates in a table avoiding the use of Oracle analytic functions and subqueries.
 
I have a table of product sales and I will like to know how sales have been going eversince consumers started buying it(Table 1). I need to get the minimum date of each product when sales started and subtract it from successive dates and show the relative sale. I know I can achieve it using analytic functions like ( DENSE_RANK FIRST ORDER BY)  or subqueries.  If possible I will like  to avoid it  . The result I will like to have is available in Table 2

Period      Prod       Sale
200810      ref              2
200811      ain              6
200811      ref              3
200812      ain              7
200812      del              3
200901      ain              9
200901      ref              5
200901      del             4
            
Table 1

Here is the result I will like to expect.

Prod      Period      Min_Date      Month_Diff      Sale
ref      200810      200810      0                       2
ain      200811      200811      0                       6
ref      200811      200810      1                        3
ain       200812      200811      1                        7
del      200812      200812      0                        3
ref      200901      200810      3                        5
del      200901      200812      4                        5
Table 2

DB: Oracle 10g
0
Comment
Question by:diteps06
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39945206
why are you trying to avoid analytic functions?
0
 
LVL 1

Author Comment

by:diteps06
ID: 39945304
The reason is that there will be a group by in the final query and analytic functions aren't compatible with it
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 225 total points
ID: 39945537
what is the final query you're trying to achieve?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Expert Comment

by:awking00
ID: 39945564
A few questions.
Why is there no record for product ain for period 200901 in your expected results?
Why do you show a month_diff of 4 and a sale of 5 for prod del for period 200901 and a min_date of 200812 in your expected results?
What do you gain with a group by statement in your final query that can't be accomplished with a partition by clause in an analytical query?
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 39945569
Also, given your sample data, what do you expect as the end result of your "final" query?
0
 
LVL 1

Author Comment

by:diteps06
ID: 39945752
"Why is there no record for product ain for period 200901 in your expected results?"
Sorry ain had to be in the expected result

"Why do you show a month_diff of 4 and a sale of 5 for prod del for period 200901"
Sorry again for the error
" and a min_date of 200812 in your expected results?"
del has minimum date 200812 is the first record in the table. The minimum date represents the first month is starts its sale which is the first time it appears in the table.
There can be a month where there is no sale (ref in 200812)

Here is the updated version  of the expected result:
Prod      Period        Min_Date      Month_Diff     Sale
ref         200810      200810          0                     2
ain        200811       200811          0                    6
ref        200811       200810          1                     3
ain       200812       200811           1                    7
del       200812       200812           0                    3
ref       200901       200810           3                     5
del      200901       200812           1                     4
ain      200901       200811            2                    9
Table 2

"What do you gain with a group by statement in your final query that can't be accomplished with a partition by clause in an analytical query?"
The monthly is a sum of the daily sales. In the query I have to use SUM to get the total in a month. Consequently there will be a group by clause.
Group by clause and analytic functions aren't compatible.

If there are no other alternative we can try subqueries.
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 225 total points
ID: 39945955
>>> Group by clause and analytic functions aren't compatible.

why do you say that?

what are you "really" trying to do?
0
 
LVL 32

Expert Comment

by:awking00
ID: 39946005
"select sum(daily_sales) over (partition by prod, period order by period) as monthly_sales"
will create a sum of daily_sales for each prod and period without requiring a group by clause.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 175 total points
ID: 39946992
Another question. What is the data type of "period"?

You refer to this field as dates ("can I calculate the differences of two dates") but they aren't actually dates that are shown. They could be integers or strings.

Below they have been interpreted as integers, so you can see the importance of knowing what that field really is.
| PROD | PERIOD | MIN_PERIOD | T.PERIOD-INQ.MIN_PERIOD | SALE |
|------|--------|------------|-------------------------|------|
|  ain | 200811 |     200811 |                       0 |    6 |
|  ain | 200812 |     200811 |                       1 |    7 |
|  ain | 200901 |     200811 |                      90 |    9 |
|  del | 200812 |     200812 |                       0 |    3 |
|  del | 200901 |     200812 |                      89 |    4 |
|  ref | 200810 |     200810 |                       0 |    2 |
|  ref | 200811 |     200810 |                       1 |    3 |
|  ref | 200901 |     200810 |                      91 |    5 |

SELECT
        t.prod
      , t.period
      , inq.min_period
      , t.period - inq.min_period
      , t.sale
FROM table1 t
INNER JOIN (
            SELECT
                  prod
                , min(period) AS min_period
            FROM table1
            GROUP BY prod
           ) inq ON t.prod = inq.prod
ORDER BY
        t.prod
      , t.period
;

http://sqlfiddle.com/#!4/838a6/8

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39954012
B penalty grade is not appropriate here, please explain or RA for moderator to correct.
0
 
LVL 1

Author Comment

by:diteps06
ID: 39959408
RA
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example, show how to take different types of Oracle backups using RMAN.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 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