Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Subquery headache with Count and GroupBy
I'm trying to return an integer from the following table that returns
the number of unique cities:


tblEmployees
Name     City
John       Boston
Frank     New York
Jim         Omaha
Betty      New York

The answer should be 3.

DECLARE @EmployeeCities int

SELECT @EmployeeCities = SELECT ... ???

How go I return one row/column into @EmployeeCities from a Count and a
GroupBy?

Headache already... Maybe it's too early...


Report this thread to moderator Post Follow-up to this message
Old Post
laurenq uantrell
11-22-05 04:23 PM


Re: Subquery headache with Count and GroupBy
laurenq uantrell  wrote:
> I'm trying to return an integer from the following table that returns
> the number of unique cities:
>
>
> tblEmployees
> Name     City
> John       Boston
> Frank     New York
> Jim         Omaha
> Betty      New York
>
> The answer should be 3.
>
> DECLARE @EmployeeCities int
>
> SELECT @EmployeeCities = SELECT ... ???
>
> How go I return one row/column into @EmployeeCities from a Count and a
> GroupBy?
>
> Headache already... Maybe it's too early...

select @EmployeeCities = select count(distinct City) from tblEmployees

Kind regards

robert


Report this thread to moderator Post Follow-up to this message
Old Post
Robert Klemme
11-22-05 04:23 PM


Re: Subquery headache with Count and GroupBy
Jees! I'd better start drinking coffee in the morning! Thanks. I must
have wasted 30 minutes trying to put a GROUPBY subquery inside a COUNT
subquery before seeking help!


Report this thread to moderator Post Follow-up to this message
Old Post
laurenq uantrell
11-22-05 04:23 PM


Re: Subquery headache with Count and GroupBy
laurenq uantrell  wrote:
> Jees! I'd better start drinking coffee in the morning!

:-))

> Thanks.

In fact there was a typo - 1 "select" too much:

select @EmployeeCities = count(distinct City) from tblEmployees

> I must
> have wasted 30 minutes trying to put a GROUPBY subquery inside a COUNT
> subquery before seeking help!

You mean like

select @EmployeeCities = count(*) from (select City from tblEmployees
group by City) x

?

Cheers

robert


Report this thread to moderator Post Follow-up to this message
Old Post
Robert Klemme
11-22-05 04:23 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 03:40 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006