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
 get value from two tabels with same where class

Author  Topic 

cutepraba
Yak Posting Veteran

53 Posts

Posted - 2012-07-21 : 12:30:11
I have tables called job and jobhistory and i need to get sum of total from both with same where class. All the fields are same in both table.

like this:
select sum(total) from jobhistory where jobdate = '7/2/2012' and id=35
+select sum(total) from job where jobdate = '7/2/2012' and id=35

Is there any optimized query to get that?




____________
Praba

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-21 : 12:35:31
[code]
select j.id,j.jobdate,sum(j.total) + jh.histtotal
from job j
inner join (select id,jobdate,sum(total) as histtotal
from jobhistory
group by id,jobdate)jh
on j.jobdate = jh.jobdate
and j.id = jh.id
group by j.id,j.jobdate,jh.histtotal
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-07-22 : 16:55:19
[code]
select sum(a.total) as total
from (select sum(total) as total from jobhistory where jobdate = '7/2/2012' and id=35
union all
select sum(total) as total from job where jobdate = '7/2/2012' and id=35
) as a
[/code]
Go to Top of Page
   

- Advertisement -