Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
SQL help

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
kalidasvs

New User


Joined: 08 Feb 2018
Posts: 11
Location: US

PostPosted: Wed Mar 21, 2018 5:50 am    Post subject: SQL help
Reply with quote

I have a table as below:

Order#, item #, Total Order Amount
Order#, item #, Total Order Amount
Order#, item #, Total Order Amount

Order-1, Item-1, 500.00
Order-1, Item-3, 500.00
Order-1, Item-4, 500.00
Order-2, Item-2, 800.00
Order-2, Item-4, 800.00
Order-3, Item-4, 900.00
Order-3, Item-5, 900.00

In this table, there can be multiple rows for each Order number based on number of items on the order. However, each row has the Total order amount.

I need to find the sum of all orders. For each order only one row should be considered. Otherwise, the amount gets added multiple times.

I wrote query as below:

SELECT SUM(ORD_AMT)
FROM ORDER_TAB A
ITEM_NUM = (SELECT MIN(ITEM_NUM) FROM ORDER_TAB
WHERE ORDER_NUM = A.ORDER_NUM)

Expected result = 2200.00

I wrote this query assuming that the subquery returns minimum item number which causes only one row per order is considered in finding total amount. However, when I submitted trough SPUFI, the query is running for very long time and I had to cancel the session. Please help me resolve this. Unfortunately, I coded SQL many years ago so I lost touch.
Back to top
View user's profile Send private message

dneufarth

Active User


Joined: 27 Apr 2005
Posts: 282
Location: Cincinnati OH USA

PostPosted: Wed Mar 21, 2018 7:34 am    Post subject:
Reply with quote

https://www.ibm.com/support/knowledgecenter/en/SS9UMF_12.1.0/ref/ref/tpc/dsq_distinct.html
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 2037
Location: NY,USA

PostPosted: Wed Mar 21, 2018 8:29 pm    Post subject:
Reply with quote

Its so very basic.
Code:
select sum(distinct(Order Amount)) from table group by order#

*provided the amount per order always duplicate for all the items as per the ex you provided.
Back to top
View user's profile Send private message
kalidasvs

New User


Joined: 08 Feb 2018
Posts: 11
Location: US

PostPosted: Wed Mar 21, 2018 9:04 pm    Post subject:
Reply with quote

Rohit Umarjikar wrote:
Its so very basic.
Code:
select sum(distinct(Order Amount)) from table group by order#

*provided the amount per order always duplicate for all the items as per the ex you provided.


This does not work. I need the total of order amounts from each of the distinct orders. Your query only considers unique amounts instead of unique order numbers. If two different orders have same order amount, then result will be wrong.
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 2037
Location: NY,USA

PostPosted: Wed Mar 21, 2018 9:07 pm    Post subject:
Reply with quote

Do you know what is Group by?
please clear up the concepts.
Back to top
View user's profile Send private message
kalidasvs

New User


Joined: 08 Feb 2018
Posts: 11
Location: US

PostPosted: Wed Mar 21, 2018 9:18 pm    Post subject:
Reply with quote

Yes. I use Group by.

Rohit Umarjikar wrote:
Do you know what is Group by?
please clear up the concepts.
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 2037
Location: NY,USA

PostPosted: Wed Mar 21, 2018 9:20 pm    Post subject:
Reply with quote

You all need is just another select on top if it to sum the total of the above query. to get 2200
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:


Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us