SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 06-28-2007, 10:24 PM
MapGeek MapGeek is offline
Junior Member
 
Join Date: Jun 2007
Location: Northern California
Posts: 5
MapGeek is on a distinguished road
Default ANY dialect question:

How to COUNT records in UNION?


I've checked several references and can't find any syntax that works. Doing a UNION of two SELECT queries pulling the same field from two diferent tables:

SELECT fld1 FROM tbl1 WHERE [expression] UNION SELECT fld1 FROM tbl2 WHERE [expression]

I just want to count the number of records in the result set, using SELECT COUNT(*). Not sure what the right syntax is, but have a feeling there's something easy that I'm missing, maybe another way without using UNION. Any help much appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-01-2007, 06:53 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to COUNT records in UNION?

select count(*) from
(SELECT fld1 FROM tbl1 WHERE [expression] UNION SELECT fld1 FROM tbl2 WHERE [expression])
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-02-2007, 05:09 AM
MapGeek MapGeek is offline
Junior Member
 
Join Date: Jun 2007
Location: Northern California
Posts: 5
MapGeek is on a distinguished road
Default ANY answer. Re: How to COUNT records in UNION?

Yes, that's what I thought of first and tried immediately. But it generates a syntax error. The problem is UNION. This works:

SELECT COUNT(*) FROM (SELECT ...)

where the subquery is simple. Use UNION in the subquery, though, and it chokes.

Is this possibly a SQL Server/T-SQL-specific problem? I can't find any clarification on MS's SQL Server Books Online, but they give no SELECT examples incorporating a subquery with UNION like this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-02-2007, 12:51 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to COUNT records in UNION?

select count(*) from (select .......)
union
select count(*) from (select.....);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-02-2007, 06:42 PM
MapGeek MapGeek is offline
Junior Member
 
Join Date: Jun 2007
Location: Northern California
Posts: 5
MapGeek is on a distinguished road
Default ANY answer. Re: How to COUNT records in UNION?

I appreciate the suggestions, but this returns two records, each containing the count for one of the SELECT statements. I need a single record containing the count for all the records in the UNION of the two statements. Again, I found a workaround, so it's not urgent. It's just bugging me, because it seems there ought to be a way to do this.

Incidentally, so as not to mislead anyone reading this, in my previous reply I was wrong about SELECT COUNT(*) FROM (SELECT ...). It also generates a syntax error. Maybe you just can't use the COUNT function with a subquery in any way, at least with SQL Server.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-02-2007, 08:37 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: How to COUNT records in UNION?

Quote:
Originally Posted by MapGeek View Post
Incidentally, so as not to mislead anyone reading this, in my previous reply I was wrong about SELECT COUNT(*) FROM (SELECT ...). It also generates a syntax error. Maybe you just can't use the COUNT function with a subquery in any way, at least with SQL Server.
MapGeek,

Could you try aliasing the subquery:
SELECT COUNT(*) FROM (SELECT ...) as u

I am not a SQL Server user so I might be completely wrong in my guess.

Hope this helps.
Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-02-2007, 08:55 PM
MapGeek MapGeek is offline
Junior Member
 
Join Date: Jun 2007
Location: Northern California
Posts: 5
MapGeek is on a distinguished road
Default ANY answer. Re: How to COUNT records in UNION?

Dimitar,

Yup, that did the trick, with both a plain subquery and a UNION in the subquery. Thanks very much for the help!

MapGeek
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-26-2007, 03:03 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to COUNT records in UNION?

Quote:
Originally Posted by Dimitar View Post
MapGeek,

Could you try aliasing the subquery:
SELECT COUNT(*) FROM (SELECT ...) as u

I am not a SQL Server user so I might be completely wrong in my guess.

Hope this helps.
Dimitar
dUDE u saved my day
AD
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-26-2007, 06:02 PM
Unregistered
 
Posts: n/a
Smile ANY answer. Re: How to COUNT records in UNION?

MapGeek,

Try this and tell me is it solving your problem or not

select sum( counter)
from (select count(*) counter from Table1
Union
select count(*) from Table2
);

Sheetal
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-04-2008, 11:46 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to COUNT records in UNION?

This works fine for MS SQL 2005

select sum(adet) from (
select count(*) as adet from (select * from Member ) as a1
union
select count(*) as adet from (select * from Member where recId=14) as a2
) as Toplam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8