Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 COUNT on UNION Result

Author  Topic 

geossl
Yak Posting Veteran

85 Posts

Posted - 2003-12-01 : 22:24:25
Dear all,
Is it possible to use one query to count the result of the following UNION?

SELECT DocID
FROM TblA
WHERE ID = 8000

UNION

SELECT DocID
FROM TblB
WHERE Type = 'B'


Thanks.

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2003-12-01 : 22:33:15
[code]
SELECT COUNT(*)
FROM
(
SELECT DocID
FROM TblA
WHERE ID = 8000

UNION

SELECT DocID
FROM TblB
WHERE Type = 'B'
) AS A
[/code]

By the way, UNION does a DISTINCT while UNION ALL does not.

DavidM

"SQL-3 is an abomination.."
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-12-01 : 23:08:09
If the count is all that's needed

SELECT (SELECT COUNT(DISTINCT DocID) FROM TblA WHERE ID = 8000) + (SELECT COUNT(DISTINCT DocID) FROM TblB WHERE Type = 'B')
Go to Top of Page
   

- Advertisement -