Solved

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

Posted on 2006-06-11
5
547 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:stev0931
  • 2
  • 2
5 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 150 total points
Comment Utility
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
 

Author Comment

by:stev0931
Comment Utility
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
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 150 total points
Comment Utility
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
 
LVL 5

Accepted Solution

by:
rmacfadyen earned 150 total points
Comment Utility
select
   (select count(*) from TableA) + (select count(*) from TableB) as Total

Regards,

Rob
0
 

Author Comment

by:stev0931
Comment Utility
Thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now