MySQL solution like the median salary problem

  • 1

    This solution is like the median salary problem

    select FORMAT(avg(n.Number),4)*1.0 as median 
    from Numbers n left join
    select Number, @prev := @count as prevNumber, (@count := @count + Frequency) as countNumber
    from Numbers, 
    (select @count := 0, @prev := 0, @total := (select sum(Frequency) from Numbers)) temp order by Number
    ) n2
    on n.Number = n2.Number
    (prevNumber < floor((@total+1)/2) and countNumber >= floor((@total+1)/2))
    (prevNumber < floor((@total+2)/2) and countNumber >= floor((@total+2)/2))

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.