Solved

Query that generates the life cycle of product’s orders

Posted on 2014-03-14
4
306 Views
Last Modified: 2014-03-14
I will like to produce a query which generates the life cycle of orders on  the different products present in the company.
The order of the products for each item from when it was lunched in the market is available in Table 1. The product can start having  orders from when it was lunched or  there can be a month where orders were absent (AX1). Another possibility is that the product may start having orders some few months later from it was presented over the market.

The launch dates for each product are present in table 2
I will like to have for each product the monthly order from when it was launched under a common monthly timescale distribution. The monthly distribution is an incremental month distribution from the respective launched date to present date.
Month1 is the launched date each product, Month 2 is the second month from the launched date ( eg. AX1 200810, BY1 200910) and so on. The resultset I am expecting is in Table 3.
How can I achieve it


Product Date        Order
AX1       200810   29
AX1       200812   13
AX1       200902   45
BY1       200909    5
BY1      200910     7
BY1      200911     3
BY1      200912     4
CZ1      201111     4
CZ1      201112     6
CZ1      201201     10
Table 1 Storical table of product orders

Product  LaunchDate
AX1        200809
BY1        200909
CZ1       201110
Table 2 Launch dates of products

 Product       Month1 Month2 Month3 Month4
AX1              29         13         45
BY1              5            7          3             4
CZ1                            4          6             10
Table 3 Product order's in life cycle

Database: Oracle 10g
0
Comment
Question by:diteps06
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39929450
Are you aware that you will potentially have 40 (forty) months is the header?

MAX(order_date) - MIN(launchdate) = 40 months.

Or do you only want the first 4 months = launch date + 3 months like this:

SQL> WITH Products (Product, Launchdate)
     AS (SELECT 'AX1', TO_DATE ( 200809, 'YYYYMM') FROM DUAL UNION
  2    3           SELECT 'BY1', TO_DATE ( 200909, 'YYYYMM') FROM DUAL UNION
  4           SELECT 'CZ1', TO_DATE ( 201110, 'YYYYMM') FROM DUAL)
  5     , Orders (Product, Order_Date, Ordernum)
  6       AS (SELECT 'AX1', TO_DATE ( 200810, 'YYYYMM'), 29 FROM DUAL UNION
  7           SELECT 'AX1', TO_DATE ( 200812, 'YYYYMM'), 13 FROM DUAL UNION
  8           SELECT 'AX1', TO_DATE ( 200902, 'YYYYMM'), 45 FROM DUAL UNION
  9           SELECT 'BY1', TO_DATE ( 200909, 'YYYYMM'),  5 FROM DUAL UNION
 10           SELECT 'BY1', TO_DATE ( 200910, 'YYYYMM'),  7 FROM DUAL UNION
 11           SELECT 'BY1', TO_DATE ( 200911, 'YYYYMM'),  3 FROM DUAL UNION
 12           SELECT 'BY1', TO_DATE ( 200912, 'YYYYMM'),  4 FROM DUAL UNION
 13           SELECT 'CZ1', TO_DATE ( 201111, 'YYYYMM'),  4 FROM DUAL UNION
 14           SELECT 'CZ1', TO_DATE ( 201112, 'YYYYMM'),  6 FROM DUAL UNION
 15           SELECT 'CZ1', TO_DATE ( 201201, 'YYYYMM'), 10 FROM DUAL)
 16  -- ---
 17  SELECT   Product
 18         , SUM ( DECODE (Mthb, 0, Ordernum, 0)) Launchdate
 19         , SUM ( DECODE (Mthb, 1, Ordernum, 0)) Month1
 20         , SUM ( DECODE (Mthb, 2, Ordernum, 0)) Month2
 21         , SUM ( DECODE (Mthb, 3, Ordernum, 0)) Month3
 22      FROM (SELECT P.*, O.Order_Date, O.Ordernum
 23                 , MONTHS_BETWEEN ( O.Order_Date, P.Launchdate) Mthb
 24              FROM Products P, Orders O
 25             WHERE O.Product = P.Product
 26               AND MONTHS_BETWEEN ( O.Order_Date, P.Launchdate) <= 3)
 27  GROUP BY Product
 28  ORDER BY Product
 29  /


PRODUCT   LAUNCHDATE     MONTH1     MONTH2     MONTH3
--------- ---------- ---------- ---------- ----------
AX1                0         29          0         13
BY1                5          7          3          4
CZ1                0          4          6         10

Open in new window

0
 
LVL 1

Author Comment

by:diteps06
ID: 39929496
There is no problem if there will be more than 40 months. The client is ready to expect it.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 39929613
Unfortunately in 10g you cannot "pivot" a table unless you a) hard-code all the expected column or write a procedure with dynamic sql

Even in 11.2.0.3 you need to hard-code the "pivoting" columns like this:
SQL> WITH Products (Product, Launchdate)
  2       AS (SELECT 'AX1', TO_DATE ( 200809, 'YYYYMM') FROM DUAL UNION
  3           SELECT 'BY1', TO_DATE ( 200909, 'YYYYMM') FROM DUAL UNION
  4           SELECT 'CZ1', TO_DATE ( 201110, 'YYYYMM') FROM DUAL)
  5     , Orders (Product, Order_Date, Ordernum)
  6       AS (SELECT 'AX1', TO_DATE ( 200810, 'YYYYMM'), 29 FROM DUAL UNION
  7           SELECT 'AX1', TO_DATE ( 200812, 'YYYYMM'), 13 FROM DUAL UNION
  8           SELECT 'AX1', TO_DATE ( 200902, 'YYYYMM'), 45 FROM DUAL UNION
  9           SELECT 'BY1', TO_DATE ( 200909, 'YYYYMM'),  5 FROM DUAL UNION
 10           SELECT 'BY1', TO_DATE ( 200910, 'YYYYMM'),  7 FROM DUAL UNION
 11           SELECT 'BY1', TO_DATE ( 200911, 'YYYYMM'),  3 FROM DUAL UNION
 12           SELECT 'BY1', TO_DATE ( 200912, 'YYYYMM'),  4 FROM DUAL UNION
 13           SELECT 'CZ1', TO_DATE ( 201111, 'YYYYMM'),  4 FROM DUAL UNION
 14           SELECT 'CZ1', TO_DATE ( 201112, 'YYYYMM'),  6 FROM DUAL UNION
 15           SELECT 'CZ1', TO_DATE ( 201201, 'YYYYMM'), 10 FROM DUAL)
 16  -- ---
 17  SELECT * FROM (
 18      SELECT P.*, O.Ordernum
 19           , 'Month-'||TO_CHAR(MONTHS_BETWEEN ( O.Order_Date, P.Launchdate),'FM00') Mthb
 20        FROM Products P, Orders O
 21       WHERE O.Product = P.Product)
 22  PIVOT (SUM(Ordernum) FOR Mthb IN ('Month-00','Month-01','Month-02','Month-03','Month-04','Month-05'))
 23  ORDER BY Product
 24  /

PRODUCT   LAUNCHDATE 'Month-00' 'Month-01' 'Month-02' 'Month-03' 'Month-04' 'Month-05'
--------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
AX1       09/01/2008                    29                    13                    45
BY1       09/01/2009          5          7          3          4
CZ1       10/01/2011                     4          6         10

Open in new window


On the other hand you could convince the "client" to limit the report to the first "X" months in the lifetime of the product, then you can easily hard-code that number of months...
0
 
LVL 1

Author Comment

by:diteps06
ID: 39929798
I think some modern reporting tools will do the pivot table.
The query is very good especially the expressions of nature:
SUM ( DECODE (Mthb, 1, Ordernum, 0)) Month1
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with Oracle sql and I am using Aqua studio. 8 72
PL/SQL LOOP CURSOR 3 57
Oracle SQL 6 47
PL/SQL Display based on value 4 18
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now