SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get value from two tabels with same where class
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cutepraba
Yak Posting Veteran

India
53 Posts

Posted - 07/21/2012 :  12:30:11  Show Profile  Reply with Quote
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

India
47040 Posts

Posted - 07/21/2012 :  12:35:31  Show Profile  Reply with Quote

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


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

Go to Top of Page

bitsmed
Yak Posting Veteran

Denmark
98 Posts

Posted - 07/22/2012 :  16:55:19  Show Profile  Reply with Quote

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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000