Select TOP X (or bottom) percent for numeric values in MySQL

前端 未结 3 565
时光取名叫无心
时光取名叫无心 2020-11-29 10:15

I was wondering if there are any functions that can be used in MySQL to select the TOP X(or bottom) percent from a column containing numeric values.

Basically, I ha

相关标签:
3条回答
  • 2020-11-29 10:43

    UPDATE: Much more thought-out explanation of the subject from much more knowing person here. Nonetheless, it still seems there's no embedded function in MySQL to calculate percentiles.

    Try:

    SELECT * FROM prices WHERE price >= (SELECT 0.9 * max(price) FROM prices)

    SELECT price FROM prices p1 WHERE
    (SELECT count(*) FROM prices p2 WHERE p2.price >= p1.price) <=
         (SELECT 0.1 * count(*) FROM prices)
    );
    

    This will give price P1 for which number of records in Price table having price >= P1 will be one tenth of total number of records in Price table. After that:

    SELECT * FROM prices WHERE price >= (SELECT price FROM prices p1 WHERE
    (SELECT count(*) FROM prices p2 WHERE p2.price >= p1.price) <=
         (SELECT 0.1 * count(*) FROM prices)
    );
    

    will return all desired records.

    Note: I didn't examine performance of this query, I think solution with temporary table/variable must be more effective.

    0 讨论(0)
  • 2020-11-29 10:47

    EDIT - new answer

    Answered in Convert SQL Server query to MySQL

    Select *
    from
    (
        SELECT tbl.*, @counter := @counter +1 counter
        FROM (select @counter:=0) initvar, tbl
        ORDER BY ordcolumn
    ) X
    where counter <= (50/100 * @counter);
    ORDER BY ordcolumn
    


    OLD ANSWER

    For MySQL, you could calculate the batch size required and then LIMIT to that number of records

    SELECT @rows := ROUND(COUNT(*) * 10/100) FROM table;
    PREPARE STMT FROM ‘SELECT * FROM tbl ORDER BY price LIMIT ?’;
    EXECUTE STMT USING @rows;
    

    For a bottom percent, just order in reverse

    SELECT @rows := ROUND(COUNT(*) * 10/100) FROM table;
    PREPARE STMT FROM ‘SELECT * FROM tbl ORDER BY price DESC LIMIT ?’;
    EXECUTE STMT USING @rows;
    

    Oops, maybe the DESC belongs in the first query, but you get the meaning.

    Note For SQL Server, the TOP N PERCENT clause certainly helps

    select top 10 PERCENT *
    FROM TBL
    ORDER BY price
    
    0 讨论(0)
  • 2020-11-29 10:50

    just as an FYI (i know this question is a few years old), this can be done other, cleaner ways as well.

    SELECT * FROM product_table WHERE price >= (SELECT price FROM product_table 
    ORDER BY price DESC LIMIT 1 OFFSET (SELECT 0.1 * COUNT(*) FROM product_table));
    

    i ran a similar query over a very large database, and it ran very quickly.

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