Solved

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

Posted on 2006-06-11
5
551 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
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 65

Assisted Solution

by:Jim Horn
Jim Horn earned 150 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 150 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Sever Import/export problem 7 44
SQL Sum of items in two tables not equal. 5 43
How do I figure out how much database is expected to grow? 15 72
Need sql in string 2 28
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

679 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