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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help me Query sum Count 2 table or n table

Author  Topic 

niyaz4872
Starting Member

41 Posts

Posted - 2013-01-10 : 03:21:10
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

52326 Posts

Posted - 2013-01-10 : 03:24:52
[code]
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
[/code]

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

Go to Top of Page
   

- Advertisement -