Q

Consolidating sums from two GROUP BYs

I have a union of two separate tables that are summed using a group by. I want a consolidated sum recognizing the group by.

 Table A 
  x  2
  x  1
  y  1

Table B x 2 y 3 y 2

Here is the result that i desire --

  x  5
  y  6

My sql does not work

  select colA, sum(colB) from A
   group by colA
 union
  select colA, sum(colB) from B
   group by colA

Instead of my desired results, I am getting duplicate x and y rows.

This was first published in January 2002

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close