my solution using session variables and accumulative frequency values


  • 0
    P
    select avg(number) as median FROM (SELECT Number, @accum1:= @accum1 + Frequency AS Accum1 from NUMBERS, (select @accum1:=0) as temp ORDER BY Number ) as temp4 
    
    where accum1  =  (
    
    SELECT min(Accum) as medianin FROM (
        SELECT Number, @accum:= @accum + Frequency AS Accum from NUMBERS, (select @accum:=0) as temp ORDER BY Number ) as temp3 
        WHERE Accum >= 
    (
        SELECT floor((sum(frequency)+1)/2) AS middle FROM Numbers
        
    )
    )
    
    OR  accum1  = (
    
    SELECT min(Accum2) as medianin FROM (
        SELECT Number, @accum2:= @accum2 + Frequency AS Accum2 from NUMBERS, (select @accum2:=0) as temp ORDER BY Number ) as tempP 
        WHERE Accum2 >= 
    (
        SELECT floor((sum(frequency)+2)/2) AS middle FROM Numbers
        
    )
    )
    

Log in to reply
 

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