【问题标题】:How to add(sum) the column data in SQL如何在 SQL 中添加(求和)列数据
【发布时间】:2023-04-09 15:06:01
【问题描述】:
ID  pcID    contractor  approver    claimed
-------------------------------------------
1   1       one          1000         900
2   1       two           200         100
3   1       three        1000        1000
4   1       six           100          11
5   2       six           100          22
6   3       six           120           1
7   4       three         102          10

从上表中,我需要根据承包商将批准人金额和索赔金额相加。如下表所示。一切都是通过使用存储过程完成的。

ID  pcID    contractor  approver    claimed  TotalApprover   TotalClaimed
--------------------------------------------------------------------------
1   1       one          1000         900     1000               900
2   1       two           200         100      200               100
3   1       three        1000        1000     1000              1000
4   1       six           100          11      100                11
5   2       six           100          22      200                33
6   3       six           120           1      320                34
7   4       three         102          10     1120              1001    

像上表一样,我需要根据承包商按升序添加输出(总和)。

提前致谢。

【问题讨论】:

    标签: mysql sql window-functions


    【解决方案1】:

    你可以使用窗口功能

    select t.*,
      sum(approver) over( partition by contractor order by ID) TotalApprover,
      sum(claimed) over( partition by contractor order by ID) TotalClaimed,
    from table1 t
    

    【讨论】:

      【解决方案2】:

      您想要累积总和:

      select t.*,
             sum(approver) over (partition by contractor order by claimid) as totalapprover,
             sum(claim) over (partition by contractor order by claimid) as totalclaim
      from t;
      

      这只是根据您要求的维度累积相应的值。

      【讨论】:

      • 感谢您的努力。它工作正常人......如果可能的话,你能解释一下先生吗?
      猜你喜欢
      • 2018-10-03
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2011-01-27
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      相关资源
      最近更新 更多