How do I "add" 2 select count(*) statements?

How do I execute a statement like this:

SELECT COUNT(*) FROM [User].Users + SELECT COUNT(*) FROM [Survey].Users

The idea being that it adds the number of users in schema user and the number of users in schema survey and returns the sum

Another example of what might be executed without involving schemas:
SELECT COUNT(*) FROM ClassApplications + SELECT COUNT(*) FROM CourseApplications


Note that these statements do not execute (syntax error).  My question is: How would I word the above statements to get the behavior I'm looking for?
stev0931Asked:
Who is Participating?
 
rmacfadyenCommented:
select
   (select count(*) from TableA) + (select count(*) from TableB) as Total

Regards,

Rob
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I'm sure there is a more elegant/correct way to do this, but this worked in my Query Analyzer

Select a.FirstTable + b.SecondTable
FROM
(SELECT COUNT(User.Users) as FirstTable FROM User) a
CROSS JOIN
(SELECT COUNT(Survey.Users) as SecondTable FROM Survey) b
0
 
stev0931Author Commented:
Thanks!  I'm assuming the CROSS JOIN is joining 2 scalars?  And, thus, there will be a negligible impact on performance, even with thousands or millions of records per table?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You'd have to test, as I'm not sure, although my guess is if you join the two tables in any other way, the join type may affect the counts returned.
0
 
stev0931Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.