Simple way to calculate median with MySQL

后端 未结 30 1137
北荒
北荒 2020-11-22 04:20

What\'s the simplest (and hopefully not too slow) way to calculate the median with MySQL? I\'ve used AVG(x) for finding the mean, but I\'m having a hard time fi

相关标签:
30条回答
  • 2020-11-22 04:46

    I just found another answer online in the comments:

    For medians in almost any SQL:

    SELECT x.val from data x, data y
    GROUP BY x.val
    HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2
    

    Make sure your columns are well indexed and the index is used for filtering and sorting. Verify with the explain plans.

    select count(*) from table --find the number of rows
    

    Calculate the "median" row number. Maybe use: median_row = floor(count / 2).

    Then pick it out of the list:

    select val from table order by val asc limit median_row,1
    

    This should return you one row with just the value you want.

    Jacob

    0 讨论(0)
  • 2020-11-22 04:46

    After reading all previous ones they didn't match with my actual requirement so I implemented my own one which doesn't need any procedure or complicate statements, just I GROUP_CONCAT all values from the column I wanted to obtain the MEDIAN and applying a COUNT DIV BY 2 I extract the value in from the middle of the list like the following query does :

    (POS is the name of the column I want to get its median)

    (query) SELECT
    SUBSTRING_INDEX ( 
       SUBSTRING_INDEX ( 
           GROUP_CONCAT(pos ORDER BY CAST(pos AS SIGNED INTEGER) desc SEPARATOR ';') 
        , ';', COUNT(*)/2 ) 
    , ';', -1 ) AS `pos_med`
    FROM table_name
    GROUP BY any_criterial
    

    I hope this could be useful for someone in the way many of other comments were for me from this website.

    0 讨论(0)
  • 2020-11-22 04:47

    You could use the user-defined function that's found here.

    0 讨论(0)
  • 2020-11-22 04:47

    My solution presented below works in just one query without creation of table, variable or even sub-query. Plus, it allows you to get median for each group in group-by queries (this is what i needed !):

    SELECT `columnA`, 
    SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(`columnB` ORDER BY `columnB`), ',', CEILING((COUNT(`columnB`)/2))), ',', -1) medianOfColumnB
    FROM `tableC`
    -- some where clause if you want
    GROUP BY `columnA`;
    

    It works because of a smart use of group_concat and substring_index.

    But, to allow big group_concat, you have to set group_concat_max_len to a higher value (1024 char by default). You can set it like that (for current sql session) :

    SET SESSION group_concat_max_len = 10000; 
    -- up to 4294967295 in 32-bits platform.
    

    More infos for group_concat_max_len: https://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_group_concat_max_len

    0 讨论(0)
  • 2020-11-22 04:48

    Optionally, you could also do this in a stored procedure:

    DROP PROCEDURE IF EXISTS median;
    DELIMITER //
    CREATE PROCEDURE median (table_name VARCHAR(255), column_name VARCHAR(255), where_clause VARCHAR(255))
    BEGIN
      -- Set default parameters
      IF where_clause IS NULL OR where_clause = '' THEN
        SET where_clause = 1;
      END IF;
    
      -- Prepare statement
      SET @sql = CONCAT(
        "SELECT AVG(middle_values) AS 'median' FROM (
          SELECT t1.", column_name, " AS 'middle_values' FROM
            (
              SELECT @row:=@row+1 as `row`, x.", column_name, "
              FROM ", table_name," AS x, (SELECT @row:=0) AS r
              WHERE ", where_clause, " ORDER BY x.", column_name, "
            ) AS t1,
            (
              SELECT COUNT(*) as 'count'
              FROM ", table_name, " x
              WHERE ", where_clause, "
            ) AS t2
            -- the following condition will return 1 record for odd number sets, or 2 records for even number sets.
            WHERE t1.row >= t2.count/2
              AND t1.row <= ((t2.count/2)+1)) AS t3
        ");
    
      -- Execute statement
      PREPARE stmt FROM @sql;
      EXECUTE stmt;
    END//
    DELIMITER ;
    
    
    -- Sample usage:
    -- median(table_name, column_name, where_condition);
    CALL median('products', 'price', NULL);
    
    0 讨论(0)
  • 2020-11-22 04:48

    Often, we may need to calculate Median not just for the whole table, but for aggregates with respect to our ID. In other words, calculate median for each ID in our table, where each ID has many records. (good performance and works in many SQL + fixes problem of even and odds, more about performance of different Median-methods https://sqlperformance.com/2012/08/t-sql-queries/median )

    SELECT our_id, AVG(1.0 * our_val) as Median
    FROM
    ( SELECT our_id, our_val, 
      COUNT(*) OVER (PARTITION BY our_id) AS cnt,
      ROW_NUMBER() OVER (PARTITION BY our_id ORDER BY our_val) AS rn
      FROM our_table
    ) AS x
    WHERE rn IN ((cnt + 1)/2, (cnt + 2)/2) GROUP BY our_id;
    

    Hope it helps

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