Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL mode (as in mean, median,mode)

I'm trying to calculate the mode of a series of idsofInterest in a table, each with an accompanying valueOfInterest such:

idsOfInterest | valueOfInterest  
2             | 1A  
2             | 1A  
2             | 3B  
1             | 2A  
1             | 2C  
1             | 2A  
4             | 3B  
4             | 3B  
4             | 4C  

but with millions of rows.
Each list of idOfInterest is sufficiently long that multimodes are not a problem. Ideally, I would like something like

idsOfInterest | modeValueOfInterest  
1             | 2A  
2             | 1A  
3             | 3C  
4             | 3B
like image 908
Michael Plazzer Avatar asked May 08 '14 03:05

Michael Plazzer


People also ask

How do you find the mean median and mode in SQL?

There's no dedicated function to calculate the median in SQL, so users often calculate the median manually. To do this, sort the data within the program in descending order. From this, you can select the top 50 percent of the data and select the last value.

Is there a mode function in SQL?

The MODE function returns the mode (the most frequently occurring value) of a numeric expression. When there are no duplicate values in the data, then MODE returns NA . The numeric expression whose mode is to be calculated.

What is mode in SQL?

Mode of a data set is the value that appears most frequently in a series of data. Example: Input: 1, 2, 2, 3, 4, 4, 4, 5, 5, 6 Output: 4 Explanation: 2 occurs 2 times, 5 occurs 2 times 4 occurs 3 times and rest other occurs 1 time So, 4 occurs most frequently and is thus the output.

How do you find the mode value in SQL?

Simply take the average of the 2 values appearing in the middle of the data set. The mode for a data set is the item(s) that appear most frequently. To calculate this by hand, you write a distinct list of values and count the number of times a value appears. The value the appears the most is your mode.


1 Answers

The mode is the most common value. You can get this with aggregation and row_number():

select idsOfInterest, valueOfInterest
from (select idsOfInterest, valueOfInterest, count(*) as cnt,
             row_number() over (partition by idsOfInterest order by count(*) desc) as seqnum
      from table t
      group by idsOfInterest, valueOfInterest
     ) t
where seqnum = 1;
like image 109
Gordon Linoff Avatar answered Sep 30 '22 06:09

Gordon Linoff