0

I have a table with column 'Price' and would need to get the most frequent value. What would be the eeasiest way?

John V
  • 4,855
  • 15
  • 39
  • 63

2 Answers2

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:

  1. Step one: make distinct selection as a collection;
  2. Step two: foreach item in distinct collection count the items found in the original collection as diffcollection;
  3. Step three: select max from diffcollection.
Andries
  • 1,547
  • 10
  • 29