Ask the Expert

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

    Requires Free Membership to View

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
  select colA, sum(colB) from B
   group by colA

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

My understanding of recent developments in the SQL standard is that you should be able to write a query like this --

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

This construction has something to do with SQL being orthogonal or whatever the word is. Any place where you can refer to a table, you should be able to write a subquery that produces a result set -- which is, as we all know, just another table. So the above should work, even though I have never tried it. Please let me know if it doesn't (and on which database you tried it).

By the way, you need UNION ALL and not UNION -- if some value of colA had the same sum in Table A as Table B, the duplicate row would be eliminated. Also, note that you could write it so that the UNION ALL involves two SELECTs that do not use GROUP BY, leaving the grouping and summing to the outer query.

A method that will work involves creating a temporary table into which you insert the intermediate results --

  create table TempSums
     ( colA  char
     , colB  integer )

  insert into TempSums
    select colA, sum(colB) from A
    group by colA

  insert into TempSums
    select colA, sum(colB) from B
    group by colA

Then you derive your consolidated results from the temporary table --

  select colA, sum(colB) from TempSums
   group by colA

For More Information

  • What do you think about this answer? E-mail the edtiors at with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was first published in January 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: