I have a table with column 'Price' and would need to get the most frequent value. What would be the eeasiest way?
Asked
Active
Viewed 3,255 times
2 Answers
2
One option would be something like
SELECT price
FROM (SELECT price, rank() over (order by cnt desc) rnk
FROM (SELECT price, count(*) cnt
FROM your_table
GROUP BY price))
WHERE rnk = 1
If there are two (or more) prices that occur equally as often, both will be returned by this query. If you want to guarantee a single row, you'll need to tell us how you want to handle ties.

Justin Cave
- 227,342
- 24
- 367
- 384
0
My algorithm is as follows:
- Step one: make distinct selection as a collection;
- Step two: foreach item in distinct collection count the items found in the original collection as diffcollection;
- Step three: select max from diffcollection.

Andries
- 1,547
- 10
- 29
-
1This can be done with a single SQL statement... – Mar 28 '13 at 09:27
-
But, does the algorithm differ? – Andries Mar 28 '13 at 09:33
-
No, but it's less efficient. – Mar 28 '13 at 09:38