在一个查询中将薪金总额与年薪增长相加-SQL PostgreSQL

时间:2018-12-01 11:18:11

标签: sql postgresql sum

我有一个基本表格,详细列出了员工和年薪。

我想计算3年薪水后企业的总成本,但是每年的薪金涨幅为3%,从今天的涨薪开始到现在的3年结束。

因此,方法应为(SUM of SALARY * 1.03) + (THAT TOTAL * 1.03) + (THIRD TOTAL * 1.03) = 3年费用,包括每年的加薪。

我尝试了以下操作(y代表年份):

SELECT ((SUM("salary")  * 1.03 AS "y2") + ("y2" * 1.03 AS "y3"), + ("y3" * 1.03)) AS "totalCost" 
FROM public."groundStaff";

出现以下错误:

ERROR:  syntax error at or near "AS"
LINE 1: SELECT ((SUM("salary")  * 1.05 AS "s2") + ("s2" * 1.05 AS "s...
                                       ^
SQL state: 42601
Character: 32

我也尝试过移动括号,但无济于事。

非常感谢!

1 个答案:

答案 0 :(得分:1)

您可以这样做:

select sum(salary) as year_0,
       sum(salary) * 1.03 as year_1,
       sum(salary) * 1.03 * 1.03 as year_2,
       sum(salary) * 1.03 * 1.03 * 1.03 as year_3
from public."groundStaff";

或者,如果您想要很多年,可以将它们放在单独的行中:

select gs.y,
       sum(salary) * power(1.03, gs.y) as salary
from public."groundStaff" cross join
     generate_series(0, 3) as gs(y)
group by gs.y    
order by gs.y;