I'm looking for the best way to implement the statistical mode calculation in SQL.
Mode is value in a list that occurs the most often. For example I have a table (Temp) that has a column (TempCol) with the following rows:
a
b
a
c
a
a
The mode of these rows would be 'a'.
This is the code that I have so far, is it the best?
select distinct_values.tempcol
from (select tempcol, count(tempcol) occurence
from temp
group by tempcol) distinct_values
where distinct_values.occurence =
(select max(distinct_values.occurence)
from (select tempcol, count(tempcol) occurence
from temp
group by tempcol) distinct_values)
Thanks
Diane
Mode is value in a list that occurs the most often. For example I have a table (Temp) that has a column (TempCol) with the following rows:
a
b
a
c
a
a
The mode of these rows would be 'a'.
This is the code that I have so far, is it the best?
select distinct_values.tempcol
from (select tempcol, count(tempcol) occurence
from temp
group by tempcol) distinct_values
where distinct_values.occurence =
(select max(distinct_values.occurence)
from (select tempcol, count(tempcol) occurence
from temp
group by tempcol) distinct_values)
Thanks
Diane