sql oracle query - 按计算的SUM列排序

时间:2014-02-06 18:17:59

标签: sql oracle

在Oracle中,有没有一种方法可以在desc中订购?

所以基本上在下面的输出中我想要一个有最大工资数的页面...即第一个结果应该显示58000,27500,13000的所有细节

BREAK on DEPARTMENT_ID page  nodup
         on JOB_ID skip 1 nodup
COMPUTE SUM OF SALARY ON DEPARTMENT_ID

/

DEPARTMENT_ID JOB_ID     LAST_NAME                     SALARY
------------- ---------- ------------------------- ----------
           20 MK_MAN     Hartstein                      13000
************* **********                           ----------
sum                                                     13000

DEPARTMENT_ID JOB_ID     LAST_NAME                     SALARY
------------- ---------- ------------------------- ----------
           80 SA_MAN     Russell                        14000
                         Partners                       13500

************* **********                           ----------
sum                                                     27500

DEPARTMENT_ID JOB_ID     LAST_NAME                     SALARY
------------- ---------- ------------------------- ----------
           90 AD_PRES    King                           24000

              AD_VP      Kochhar                        17000
                         De Haan                        17000

************* **********                           ----------
sum                                                     58000

希望我解释得很好.. 谢谢..

2 个答案:

答案 0 :(得分:1)

让我们通过在一个部门找到工资的SUM()来进行分析,并将其附加到常规列中。按降序排序,我们就完成了。

您仍然可以使用sql*plus报告。

SELECT DEPARTMENT_ID,
       JOB_ID,
       LAST_NAME,
       SALARY,
       SUM(SALARY) OVER (PARTITION BY DEPARTMENT_ID ORDER BY NULL) as CUMULATIVE_SUM
FROM EMPLOYEE
ORDER BY CUMULATIVE_SUM DESC,DEPARTMENT_ID;

要从SELECT中删除,

ORDER BY SUM(SALARY) OVER (PARTITION BY DEPARTMENT_ID ORDER BY NULL) DESC,DEPARTMENT_ID

答案 1 :(得分:0)

更多符合您的散文而不是您的代码,但如果您为汇总提供别名,则可以通过别名进行排序。

select somefield, sum(something) total
from whereever
where whatever
group by somefield
order by total desc