Link to home
Start Free TrialLog in
Avatar of sam15

asked on



NAME                   type
STOCK_NUMBER            VARCHAR2(20)


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)


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);
Avatar of Mike McCracken
Mike McCracken

Not a Crystal Reports question

Zone Advisor
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: 
Avatar of subratabiswas

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sam15


is this ansi sql above. we use regular sql and joins in all applications. can you rewrite using normal sql.
This is Oracle Syntax
Avatar of sam15


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.
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.
Avatar of sam15


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.
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.
Avatar of sam15


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
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).
Avatar of sam15


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.

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.
Avatar of sam15


Excellent answer!