ReportSQLQuerry_SumingQtyShipped

STOCK_ITEMS

NAME                   type
------------------------------------------------------------------------
STOCK_NUMBER            VARCHAR2(20)
.......


SHIPMENT

Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
SHIPMENT_ID                               NOT NULL NUMBER(10)
ORDER_NO                                           NUMBER(10)
SHIPMENT_DATE                                      DATE
SHIP_FROM_ORG                                      VARCHAR2(8)
SHIP_TO_ORG                                        VARCHAR2(8)
...


SHIPPED_ITEMS

Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
SHIPMENT_ID                               NOT NULL NUMBER(10)
STOCK_NUMBER                              NOT NULL VARCHAR2(20)
ITEM_NUMBER                               NOT NULL NUMBER(2)
QUANTITY_SHIPPED                                   NUMBER(8)
SHIPCODE                                           VARCHAR2(1)
created_date                                       DATE


I have the above 3 tables.

I need to report on 1 month, 6 month, 1 year, 2 year, 5 year and 10 year usage for each stock item. This is the total quantity shipped in each period. How would you write the SQL for that. Would you use subquery for each calculation and include that in main query like this or that would be slow and it better to do the whole clculations in one scan using CASE.

select stock_number, (query1) 1_mon, (query2) 6_mon, (query3) 1_yr, (query 4) 2_yr, (query5) 5_yr from stock_item;

query1 = select sum(quantity_shipped) from shipped_item a WHERE created date between trunc(sysdate) and trunc(sysdate-30);
sam15Asked:
Who is Participating?
 
subratabiswasConnect With a Mentor Commented:
with joined_table as (select stock_items.stock_number, shipment.shipment_date, shipped_items.quantity_shipped
                          from shipment, shipped_items, stock_items
                          where shipped_items.shipment_id = shipment.shipment_id
                            and stock_items.stock_number = shipped_items.stock_number),
      one_month_sum as (select stock_number, sum(quantity_shipped) as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -1)
                            group by stock_number),
      six_month_sum as (select stock_number, sum(quantity_shipped)  as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -6)
                            group by stock_number),
      one_year_sum as (select stock_number, sum(quantity_shipped) as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -12)
                            group by stock_number),
      two_year_sum as (select stock_number, sum(quantity_shipped) as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -24)
                            group by stock_number),
      five_year_sum as (select stock_number, sum(quantity_shipped) as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -60)
                            group by stock_number),
      ten_year_sum as (select stock_number, sum(quantity_shipped) as qty
                            from joined_table
                            where shipment_date > add_months(sysdate, -120)
                            group by stock_number)
select stock_items.stock_number,
       sum(one_month_sum.qty) as one_month_sum,
       sum(six_month_sum.qty) as six_month_sum,
       sum(one_year_sum.qty) as one_year_sum,
       sum(two_year_sum.qty) as two_year_sum,
       sum(five_year_sum.qty) as five_year_sum,
       sum(ten_year_sum.qty) as ten_year_sum
    from stock_items, one_month_sum, six_month_sum, one_year_sum, two_year_sum, five_year_sum, ten_year_sum
    where one_month_sum.stock_number(+) = stock_items.stock_number
      and six_month_sum.stock_number(+) = stock_items.stock_number
      and one_year_sum.stock_number(+) = stock_items.stock_number
      and two_year_sum.stock_number(+) = stock_items.stock_number
      and five_year_sum.stock_number(+) = stock_items.stock_number
      and ten_year_sum.stock_number(+) = stock_items.stock_number group by stock_items.stock_number
0
 
mlmccCommented:
Not a Crystal Reports question

mlmcc
Zone Advisor
0
 
pinkurayCommented:
You can go with Reporting aggregate functions in oracle which gives you the result for yearly , monthly and quarterly report like to get the details on sales or stocks quantity report:

check this link hope this will give all what you are trying to get:

http://www.orafusion.com/art_anlytc.htm 
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
sam15Author Commented:
is this ansi sql above. we use regular sql and joins in all applications. can you rewrite using normal sql.
0
 
subratabiswasCommented:
This is Oracle Syntax
0
 
sam15Author Commented:
kind of stange. i thought it is more of the ansi format we dont use.

Is this doing subqueries with full scan for each subquery? what index columns would help here.
0
 
subratabiswasCommented:
Resultset from the 1st sub-query shold be used in all the subsequent ones. Tuning this query will be a separate activity, but can be started by creating index on the columns used in the WHERE clauses and the GROUP BY clause. Try to see the effect of the indices by executing sql trace.
0
 
sam15Author Commented:
Query works fine. I tested it.

just a quick would you use SYSDATE or TRUNC(SYDATE) to go for full days instead of partials?

Also, do you ecommend to use CREATED_DATE for record in case user made a data entry error in shipment date.
0
 
subratabiswasCommented:
Yes, applying TRUNC function will return data for the few extra hours to include the period from the beginning of the day. FYI, by default TRUNC would round the time to the beginning of the day, but there are different (optional) formatting options to round it on different period (MONTH, YEAR etc.)

Whether to use CREATED_DATE or SHIPMENT_DATE, will depend on the application. It is possible that CREATED_DATE is maintained programatically and is free from any human data entry error. From that point of view, it appears preferable. But it is also possible that the data is being entered in batch or by a data entry operation at a time different from the actual shipment time. In that scenario, if someone wants to track the shipment by the actual time of shipping, he may not be interested in the CREATED_DATE. We are talking of two times, one when the shipping actually took place, and the other when the shipping record was entered into the system. Depending on the application, there may or may not be significant gap between them. Some users may ignore that gap, while others may be interested in one or the other. I guess, both the dates are maintained considering utility of both of them.
0
 
sam15Author Commented:
TRUNC(SYSDATE) will exclude all transaction that happened today. It will stop all activity at yesterday midnight. so when users run report today 10 times it will yield same result. I think this is cleaner instead of using SYSDATE which will use part of today and part of the day 30 or 60 days ago.
do you agree?

The only think is i would need to create an  a function based index INDEX on TRUNC(SHIPMENT_DATE).

we currently dont have any validation on shipment date nor any batch job. It is safer to use created_date which is automatically entered by system.


I am not sure why you used GROUP BY in your last query for result. The tables were grouped in your query for each before. is there anything i missed.

select stock_items.stock_number,
       sum(one_month_sum.qty) as one_month_sum,
       sum(six_month_sum.qty) as six_month_sum,
       sum(one_year_sum.qty) as one_year_sum,
       sum(two_year_sum.qty) as two_year_sum,
       sum(five_year_sum.qty) as five_year_sum,
       sum(ten_year_sum.qty) as ten_year_sum
    from stock_items, one_month_sum, six_month_sum, one_year_sum, two_year_sum, five_year_sum, ten_year_sum
0
 
subratabiswasCommented:
1) Last GROUP BY is required because the query is using AGGREGATE function
2) Effect of TRUNC on duration may change depending on where the function is being applied (before applying other functions like ADD_MONTHS etc, or after the function call).
0
 
sam15Author Commented:
The reason i asked you about the aggregate is i have a few other columns from STOCK_ITEMS table to select like STOCK_DESCRIPTIO, UNITS, etc.

I do not want to select those in the aggregate function and group by them.

I am thinking to add another subquery and join that.

I have to test this but my thinking is that you already have aggregates in ONE_MONTH_SUM, SIX_MONTH_SUM. there should be one stock number, quantity.
So when you join it tothe main stock items fields it should be ONE to ONE join.

 
0
 
subratabiswasCommented:
Yes, it should be possible to remove the aggregate (SUM and GROUP BY) on the final query, as the aggregate is already done at the feeding sub-query level. Along with the in-line views, another instance of STOCK_ITEMS can also be added to select other desired attributes from that table.
0
 
sam15Author Commented:
Excellent answer!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.