TSQL mode (as in mean, median,mode)

前端 未结 1 385
南笙
南笙 2021-01-11 11:35

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

idsOfInterest | valueOfInterest  
         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 12:09

    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;
    

    0 讨论(0)
提交回复
热议问题