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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help me Query sum Count 2 table or n table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

niyaz4872
Starting Member

Iran
38 Posts

Posted - 01/10/2013 :  03:21:10  Show Profile  Reply with Quote
following is my tables structure
table1 structure:
id_1 count_1
01 2
04 1
09 5
01 6

table2 structure:
id_2 count_2
01 1
09 5
01 6
07 1
following is sample results i wan't

id Count(count_1+count_2)
01 15
04 1
07 1
09 10

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/10/2013 :  03:24:52  Show Profile  Reply with Quote

SELECT id,
SUM(cnt) AS [Count]
FROM
(
SELECT id_1 AS id,count_1 AS cnt
FROM table1
UNION ALL
SELECT id_2,count_2
FROM table2
)t
GROUP BY id
ORDER BY id


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

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.08 seconds. Powered By: Snitz Forums 2000