如何使用SQL查找前三列总计 [英] How to find the top three column totals using SQL

查看:46
本文介绍了如何使用SQL查找前三列总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对表格中的所有列求和并找出其中的前 3 列.

Trying to sum all columns in my table and to find the top 3 of them.

列只有 10 的值.这就是为什么我试图将所有输入相加以将它们相互比较.

columns have only a value of 1 or 0. That's why I am trying to sum the all inputs to compare them with each other.

我一直坚持使用 sum() 中集成的代码排序

I've stucked with order by code integrated into sum()

SELECT (ID)
FROM Student
ORDER BY SUM(C1), SUMC(C2)...SUM(C10)
lIMIT 3

推荐答案

如果我没理解错的话,你可以用union all来计算每列的总和,然后order by代码>和<代码>限制:

If I understand correctly, you can use union all to calculate the sum for each column and then order by and limit:

select c.*
from ((select 'col1', sum(col1) as s from t) union all
      (select 'col2', sum(col2) as s from t) union all
      . . . 
      (select 'col10', sum(col10) as s from t)
     ) c
order by s desc
limit 3;

这篇关于如何使用SQL查找前三列总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆