PDA

View Full Version : SQL Help


Stealth
06-11-03, 02:30 PM
Im trying to select the top 5 rows where a value appears the most times.

so

5
4
21
5
5
4

the most would be 5, then 4, then 21 etc.

Ive searched for info using COUNT but cant find nout to help :(

Shane
06-11-03, 02:52 PM
This should work.


select top 5 count(COLUMN_NAME) as PopularItems from TABLE order by PopularItems desc

Stealth
06-11-03, 06:51 PM
nope but thanks for trying

Shane
06-12-03, 09:03 AM
Sorry, this should work.


SELECT id, COUNT(id) AS amount
FROM TABLE_NAME
GROUP BY id
ORDER BY amount DESC