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)
 return result with most common field value

Author  Topic 

sql777
Constraint Violating Yak Guru

314 Posts

Posted - 2006-01-17 : 08:15:44
Hi,

I have a table with #'s in a column, these #'s may be repeat many times in the rows. I want to get a result set that has the most popular #'s listed in order.

Table:

ID Name Price
1 abc 20000
2 aew 22000
3 was 20000
4 asw 15000
5 wek 22000
6 asdf 20000


so I want a SELECT statement that will return:

PRICE COUNT
20000 3
22000 2
15000 1



thanks in advance!

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2006-01-17 : 08:18:25
SELECT Price, COUNT(1)
FROM YourTable
GROUP BY Price
ORDER BY COUNT(1) DESC

Duane.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-17 : 08:19:56
Use GROUP BY to group it by PRICE and ORDER BY to sort it by descending order
select PRICE, count(*) as [COUNT]
from table1
group by PRICE
order by [COUNT] desc


-----------------
'KH'

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-17 : 08:29:53
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -