Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2006-06-11
Medium Priority
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?
Question by:stev0931
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
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
(SELECT COUNT(User.Users) as FirstTable FROM User) a
(SELECT COUNT(Survey.Users) as SecondTable FROM Survey) b

Author Comment

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?
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.

Accepted Solution

rmacfadyen earned 600 total points
ID: 16881386
   (select count(*) from TableA) + (select count(*) from TableB) as Total



Author Comment

ID: 16881400

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 extract information from SQL Server on Database, Connection and Server properties

722 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