Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-11
5
Medium Priority
?
557 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 600 total points
ID: 16881346
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
ID: 16881358
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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 600 total points
ID: 16881377
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 600 total points
ID: 16881386
select
   (select count(*) from TableA) + (select count(*) from TableB) as Total

Regards,

Rob
0
 

Author Comment

by:stev0931
ID: 16881400
Thanks!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

972 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