Q
Problem solve Get help with specific problems with your technologies, process and projects.

Getting the sum of two count rows returned in the union query

I tried the following query on SQL server 7.0:

SELECT SUM(Cnt) from
(
SELECT Count(SbaNumber)  As Cnt FROM SLDeals
 UNION all
SELECT  Count(SBANumber)  as Cnt FROM ChDeals
)

How would I do it if I want to get the sum of the two count rows returned in the union query?

Here is how I would write a query to get the count of a UNION-ed set of rows:

SELECT count(*) 

  FROM ( SELECT * FROM SLDeals
         UNION
         SELECT * FROM ChDeals )

For More Information

  • What do you think about this answer? E-mail the edtiors at [email protected] with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle and SQL

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close