RANK从SQL中的多个列的值总和

时间:2014-10-13 17:45:04

标签: sql oracle

所以这是我的表:

S_ID | PHY | CHEM | MATHS | BIO |
_____________________________________
S01  | 100 | 100  | 100   | 100 |
S02  | 90  | 90   | 80    |  90 |
S03  | 80  | 76   | 45    |  40 |
S04  | 70  | 91   | 71    |  65 |
S05  | 70  | 99   | 88    |  67 |
S06  | 40  | 56   | 55    |  88 |
S07  | 10  | 55   | 62    |  88 |

我需要添加所有标记,然后根据总数为所有学生ID提供等级和百分位数。

有关如何做到这一点的任何帮助?我无法使用RANK()函数而不是总数..我使用(PHY+CHEM+BIO+MATHS)作为TOTAL,然后选择ROWNUM < whatever rank i need行。

SELECT * FROM (
SELECT S_ID, (MATHS+BIO+CHEM+PHY) AS TOTAL FROM MARKS ORDER BY TOTAL DESC )
WHERE ROWNUM < 4;

2 个答案:

答案 0 :(得分:2)

当然,你可以在总数上使用rank()函数:

select S_ID, (MATHS+BIO+CHEM+PHY) AS TOTAL,
       RANK() OVER (ORDER BY MATHS+BIO+CHEM+PHY DESC) as rank
FROM MARKS
ORDER BY TOTAL DESC;

我不确定你的百分位是什么意思,但其他分析功能也应该有相似之处。

编辑:我会做&#34;百分位&#34;使用显式逻辑。类似的东西:

select s.*
from (SELECT S_ID, (MATHS+BIO+CHEM+PHY) AS TOTAL,
             RANK() OVER (ORDER BY MATHS+BIO+CHEM+PHY DESC) as rank,
             COUNT(*) OVER () as num
      FROM MARKS
    ) s
WHERE num * 60/100 <= rank
      ORDER BY TOTAL DESC;

可能还有其他功能,例如您也可以用于此目的。

答案 1 :(得分:0)

选择S_ID,(MATHS + BIO + CHEM + PHY)AS TOTAL,

   TO_CHAR(PERCENT_RANK() OVER (ORDER BY MATHS+BIO+CHEM+PHY), '9.9999')*100 as PERCENTILE

FROM MARKS

ORDER BY TOTAL DESC;