MySql Logical Order By

前端 未结 1 1621
心在旅途
心在旅途 2021-01-06 05:54

I currently have query like this:

SELECT \'id\', \'clanid\', \'name\',
       \'level\', \'exp\', \'warwinpercent\',
       \'warswon\', \'warslost\', \'wars         


        
相关标签:
1条回答
  • 2021-01-06 06:17

    I suggest using ordering like in How Not To Sort By Average Rating

    PROBLEM:

    You need some sort of "score" to sort by.

    WRONG SOLUTION #1: Score = (Positive ratings) - (Negative ratings)

    WRONG SOLUTION #2: Score = Average rating = (Positive ratings) / (Total ratings)

    CORRECT SOLUTION: Score = Lower bound of Wilson score confidence interval for a Bernoulli parameter

    Demo:

    CREATE TABLE clans(id INT, name VARCHAR(100), warswon INT, warslost INT);
    
    INSERT INTO clans VALUES (1, 'aaa',  208, 6), (2, 'bbb', 103, 0);
    
    SELECT id, name,warswon, warslost,((warswon + 1.9208) / (warswon + warslost) - 
                     1.96 * SQRT((warswon * warslost) / (warswon + warslost) + 0.9604) / 
                              (warswon + warslost)) / (1 + 3.8416 / (warswon + warslost)) 
           AS ci_lower_bound 
    FROM clans 
    ORDER BY ci_lower_bound DESC;
    

    SqlFiddleDemo

    Output:

    ╔═════╦═══════╦══════════╦═══════════╦════════════════════╗
    ║ id  ║ name  ║ warswon  ║ warslost  ║   ci_lower_bound   ║
    ╠═════╬═══════╬══════════╬═══════════╬════════════════════╣
    ║  2  ║ bbb   ║     103  ║        0  ║ 0.9640439675800224 ║
    ║  1  ║ aaa   ║     208  ║        6  ║ 0.9401908847803808 ║
    ╚═════╩═══════╩══════════╩═══════════╩════════════════════╝
    
    0 讨论(0)
提交回复
热议问题