sum over

本贴最后更新于 2749 天前,其中的信息可能已经时异事殊

按部门求薪酬总和

SELECT A.*, SUM(SAL) OVER(PARTITION BY DEPTNO) SUM_DEPT_SAL
  FROM SCOTT.EMP A
 ORDER BY DEPTNO, EMPNO;

结果:

改进:一步步求和

SELECT A.*, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY SAL) SUM_DEPT_SAL
  FROM SCOTT.EMP A;

列出流水账每一笔钱支出后的余额

创建一个原始数据视图:

CREATE OR REPLACE VIEW v_t AS 
  SELECT 1000 AS ID,'预缴费用' AS NAME,30000 AS money FROM dual 
  UNION ALL 
  SELECT 7782 AS ID,'支出1' AS NAME,3450 AS money FROM dual 
  UNION ALL 
  SELECT 7839 AS ID,'支出2' AS NAME,6000 AS money FROM dual 
  UNION ALL 
  SELECT 7934 AS ID,'支出3' AS NAME,2300 AS money FROM dual;  

列出流水账每一笔钱支出后的余额

SELECT a.*,SUM(CASE  WHEN id=1000  THEN a.money ELSE -a.money END) 
  OVER(ORDER  BY ID) AS remaining FROM v_t a ORDER  BY ID;

  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    343 引用 • 723 回帖
  • select
    16 引用 • 14 回帖 • 1 关注

相关帖子

回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...