Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select SUM for two different tables

Author  Topic 

nnogueira
Starting Member

18 Posts

Posted - 2009-09-17 : 13:02:00
I need some help to make this query:

i have two Tables, like this:

TableA

EquipNum | Date | Total
00000001 | 17092009 |180,55
00000001 | 17092009 |100,87
00000002 | 17092009 |290,27
00000002 | 17092009 |30,54

TableB

EquipNum | Date | Total
00000001 | 17092009 |281,42
00000002 | 17092009 |310,81 <--- Its not correct!!! It have to be 320,81

I have to SUM and group EquipNum AND Date from TableA Like

Select SUM(Total)AS EXPR1 From TableA Group By EquipNum,Date

And compare this result from TABLEA to total from TABLEB

What i need to do ?

Thanks friends!



TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-17 : 13:12:04
The best way is to DROP TableB and whenever you need the totals just run that group by query. That way you don't need to keep them in sync.

But here is a way to find differences:

select a.EquipNum
,a.Date
,a.Expr1
,b.Total
from (
Select EquipNum,Date,SUM(Total)AS EXPR1 From TableA Group By EquipNum,Date
) a
inner join TableB b
on b.EquipNum = a.EquipNum
and b.Date = a.Date
where a.Expr1 != b.Total


Be One with the Optimizer
TG
Go to Top of Page

nnogueira
Starting Member

18 Posts

Posted - 2009-09-17 : 13:22:11
TG, thanks for help, it works.

I really need to compare this values, its to verify user entry's in different forms.. Thanks.. you helpme a lot.

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-18 : 02:30:06
What does 180,55 mean?
A csv or numeric value but comma in place of dot?


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -