Avatar of Rog D
Rog D
Flag for United States of America asked on

SQL Sum of items in two tables not equal.

Table A
Item       QOH
123         50
123         45
456           7
456         10
789          30
789          40

Table B
Item       QOH
123         50
123          45
456          4
789          28
789          40


Result Set
Item    QOH  QOH
456     17         4
789     70        68

My goal is to find the items where the Sums are NOT equal.

Thanks.
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Rog D

8/22/2022 - Mon
Kyle Abrahams

select a.item, sum(a.qoh) A_QOH, sum(b.qoh) B_QOH
from tbl_a a
join tbl_b b on a.item = b.item
having sum(a.qoh) != sum(b.qoh)

Open in new window

Rog D

ASKER
This does not work as the sums are not calculated properly.

I believe it will require some sort of sub selects?
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

Did you try an UNION?
SELECT item, SUM(QOH)
FROM tableA
GROUP BY item
UNION
SELECT item, SUM(QOH)
FROM TableB
GROUP BY item

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Rog D

ASKER
Great this solution worked with correct results.

Thanks.