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 2008 Forums
 Transact-SQL (2008)
 Adding counts of Select results

Author  Topic 

jbosco1988
Starting Member

46 Posts

Posted - 2013-04-04 : 18:11:47
I am trying to add then divide Select counts. For Example something like this:

SELECT (SELECT COUNT(*) FROM TABLE1) + (SELECT COUNT(*) FROM TABLE2)


/

SELECT (SELECT COUNT(*) FROM TABLE3) + (SELECT COUNT(*) FROM TABLE4)


where would I place the opening and ending parentheses?

Thanks to all in advance

chadmat
The Chadinator

1974 Posts

Posted - 2013-04-04 : 18:27:33
If I understand correctly, you was to divide the 2 totals, in that case, this should work:

SELECT ((SELECT COUNT(*) FROM TABLE1) + (SELECT COUNT(*) FROM TABLE2))


/

((SELECT COUNT(*) FROM TABLE3) + (SELECT COUNT(*) FROM TABLE4) )



-Chad
Go to Top of Page

jbosco1988
Starting Member

46 Posts

Posted - 2013-04-04 : 19:08:08
Ok thanks Chad for the quick response!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-05 : 01:11:12
if you want decimal portion also it should be

SELECT ((SELECT COUNT(*) FROM TABLE1) + (SELECT COUNT(*) FROM TABLE2)) * 1.0


/

((SELECT COUNT(*) FROM TABLE3) + (SELECT COUNT(*) FROM TABLE4) )


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -