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)
 help with query

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2004-08-24 : 15:15:13

Hi,

I have a table called "tblUsers". I have 200,000 records and I want to determine which are the top 10 most common cities. How can I accomplish this ?

"tblUsers"
UserID, city, otherdata-notreleveant




Thanks alot!
mike123

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-08-24 : 15:38:21
[code]
SELECT TOP 10 city
FROM
(
SELECT city, COUNT(city) AS Rank
FROM Users
GROUP BY city
) t
ORDER BY Rank DESC
[/code]

Tara
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-08-24 : 16:01:12
select top 10 city, count(distinct UserID) as no_of_users from tblUsers group by city order by count(distinct UserID) desc

/rockmoose
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-08-24 : 16:39:48
Pick your flavor


USE Northwind
GO

CREATE TABLE myTable99(City varchar(10),UserID varchar(10))
GO

INSERT INTO myTable99(City,UserId)
SELECT 'San Diego', 1 UNION ALL
SELECT 'San Diego', 2 UNION ALL
SELECT 'San Diego', 3 UNION ALL
SELECT 'San Diego', 4 UNION ALL
SELECT 'San Diego', 5 UNION ALL
SELECT 'New York', 1 UNION ALL
SELECT 'New York', 1 UNION ALL
SELECT 'New York', 1 UNION ALL
SELECT 'New York', 1 UNION ALL
SELECT 'New York', 1 UNION ALL
SELECT 'New York', 2 UNION ALL
SELECT 'New York', 3 UNION ALL
SELECT 'New York', 4 UNION ALL
SELECT 'New York', 5 UNION ALL
SELECT 'New York', 6
GO

SELECT TOP 10 city
FROM
(
SELECT city, COUNT(city) AS Rank
FROM myTable99
GROUP BY city
) t
ORDER BY Rank DESC

select top 10 city, count(distinct UserID) as no_of_users
from myTable99
group by city
order by count(distinct UserID) desc
GO

DROP TABLE myTable99
GO





Brett

8-)
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2004-08-24 : 18:25:40
thanks !
Go to Top of Page
   

- Advertisement -