SQL rank percentile

后端 未结 2 1372
星月不相逢
星月不相逢 2021-01-29 10:05

I\'ve made an SQL query which rank pages by how many times they have been viewed. For instance,

╔══════╦═══════╗
║ PAGE ║ VIEWS ║
╠══════╬═══════╣
║ J    ║   10         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 10:19

    SELECT  page, 
            views,
            (1-ranks/totals)*100 Percentile
    FROM
    (
        SELECT  page,
                views,
                @rank:=@rank + 1 ranks,
                (SELECT COUNT(*) FROM tableName) totals
        FROM    tableName a,
                (SELECT @rank:=0) s
        ORDER   BY views DESC
    ) s
    
    • SQLFiddle Demo

提交回复
热议问题