一列加上其他重复的值

时间:2019-07-01 19:02:09

标签: sql

我在汇总数据库中的工资成本时遇到了麻烦,该表似乎包含其他数据,因此该年度的净额总计为该帐户中每个条目的总单独行数。我猜在这里

我已经尝试对会计年度的净额进行总计,我将需要在过去两年中完成此操作。但首先是2017年

select account, sum(netamount ) as salaryTotal, fiscalyear 
from hargrade_projectlink.twgltransaction 
where account = 50005 and fiscalyear = 2017 
group by account, creditamount, debitamount, netamount, fiscalyear 
order by fiscalyear 

预期:

accout    salaryTotal  fiscalyear
actnum     sumtotal     2017

结果:

accout  salaryTotal   fiscalyear
actnum     sumtotal     2017
actnum     sumtotal     2017
actnum     sumtotal     2017
actnum     sumtotal     2017
actnum     sumtotal     2017
actnum     sumtotal     2017
actnum     sumtotal     2017
etc and so on

3 个答案:

答案 0 :(得分:2)

尝试如下操作,从分组依据中删除creditamount, debitamount, netamount

select account, sum(netamount ) as salaryTotal, fiscalyear 
from hargrade_projectlink.twgltransaction 
where account = 50005 and fiscalyear = 2017 
group by account,  fiscalyear 
order by fiscalyear 

答案 1 :(得分:0)

我怀疑您想要进行最终的解决方案条件汇总,如下所示:

select account,
       sum(case when fiscalyear = 2017 then netamount end) as salaryTotal_2017,
       sum(case when fiscalyear = 2018 then netamount end) as salaryTotal_2018
from hargrade_projectlink.twgltransaction 
where account = 50005 
group by account;

答案 2 :(得分:0)

尝试一下

          select account, fiscalyear ,sum(netamount ) as salaryTotal
          from hargrade_projectlink.twgltransaction 
          where account = 50005 and fiscalyear = 2017 
          group by account, fiscalyear 
          order by fiscalyear