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
 SQL Server Development (2000)
 Getting other columns from GROUP BY SELECT statment

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-08-16 : 09:29:41
Dror writes "Hi

I have the following table with existing rows

CITY_NAME----------CITY_CODE
NEW-YORK-----------10
LONDON-------------11
AMSTERDAM----------12
AMSTERDAM----------12
AMSTERDAM----------12
LONDON-------------11

Each CITY_NAME corresponds to a unique CITY_CODE

I need to get a list of each city and the count of how many times it shows up in the table.

To make the query fast I GROUP BY CITY_CODE and NOT CITY_NAME as follows:

SELECT CITY_CODE, COUNT(*) FROM table GROUP BY CITY_CODE

I want the result to be (without getting the name of the city from another table)

NEW-YORK ----- 1
AMSTERDAM----- 3
LONDON---------2

Can I get the desired result from a query that will do the comparisons on the CITY_ID (FASTER!!!) an not on the CITY_NAME

Thanks"
   

- Advertisement -