Q
Problem solve Get help with specific problems with your technologies, process and projects.

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 last published in January 2002

Dig Deeper on Oracle and SQL

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.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close