Avatar of sam15
sam15
 asked on

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);
Oracle DatabaseSQL

Avatar of undefined
Last Comment
sam15

8/22/2022 - Mon
Mike McCracken

Not a Crystal Reports question

mlmcc
Zone Advisor
pinkuray

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 
ASKER CERTIFIED SOLUTION
subratabiswas

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sam15

ASKER
is this ansi sql above. we use regular sql and joins in all applications. can you rewrite using normal sql.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
subratabiswas

This is Oracle Syntax
sam15

ASKER
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.
subratabiswas

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sam15

ASKER
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.
subratabiswas

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

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
subratabiswas

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

ASKER
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.

 
subratabiswas

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sam15

ASKER
Excellent answer!