Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to write a SQL query to get the above output

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

New User


Joined: 30 Dec 2003
Posts: 19

PostPosted: Thu May 27, 2010 10:10 pm    Post subject: How to write a SQL query to get the above output
Reply with quote

Hi

I have the following query

select sum(salary), basic, tax from the table1
union
selct sum(salary), basic,tax from the table2

the result would be like below

sum(sal) basic tax
--------------------------
7500 5000 10.5
8500 2000 11.5

But I want out put be like below

sum(sal) basic tax
------------------------
16000 70000 22.0

I have tried in several ways. Could you please let me know how to write a query to get the above output.

Thanks
Subba
Back to top
View user's profile Send private message

dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri May 28, 2010 12:11 am    Post subject:
Reply with quote

can't test it, too lazy to bring up db2,
but

Code:

select sum(t.salary) as SALARY
     , sum(t.basic)  as BASIC
     , sum(t.tax)    as TAX
from (
       select sum(salary)
            , sum(basic)
            , sum(tax)
         from table1
     union
       select sum(salary)
            , sum(basic)
            , sum(tax)
         from table2
      ) as t

Back to top
View user's profile Send private message
rocky_balboa

New User


Joined: 24 Mar 2010
Posts: 61
Location: Cape of Good Hope

PostPosted: Fri May 28, 2010 12:23 am    Post subject: Reply to: DB2 Query
Reply with quote

dbzTHEdinosauer
I am lazier than you....I was going to post about the inline view so that he could try it himself....but you have posted the entire query icon_smile.gif ...

You have made him much more lazy...
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri May 28, 2010 1:38 pm    Post subject:
Reply with quote

Probably it should be an UNION ALL so that duplicates are retained.
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:

Similar Topics
Topic Author Forum Replies Posted
No new posts Problem reading GTF trace output danik56 ABENDS & Debugging 7 Thu Mar 16, 2017 1:02 pm
No new posts SORT VSAM file with each field one by... maxsubrat DFSORT/ICETOOL 6 Tue Mar 14, 2017 1:07 pm
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts How to get current date -1 and curren... murali.andaluri DFSORT/ICETOOL 3 Wed Mar 08, 2017 2:09 pm
No new posts Write R1 and R2 to OP1 and R1 to OP2 bhaskar_kanteti DFSORT/ICETOOL 10 Fri Mar 03, 2017 11:18 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us