特定列具有相同值的表的总和

时间:2018-02-20 09:16:23

标签: sql postgresql postgresql-9.4

我试图根据月份的值找到表中的金额总和。这是我的查询

SELECT to_char(
          to_timestamp(
             date_part('month', p.date_from)::text,
             'MM'
          ),
          'Month'
       ) as Month,
       p.employee_id, p.id as payslip_id,
       p.date_from, p.date_to, pl.amount
FROM hr_payslip p
   INNER JOIN hr_payslip_line pl
      ON (p.id = pl.slip_id AND pl.code = 'NET');

输出:

enter image description here

需要:

我需要按月份和列金额的总和进行分组。

2 个答案:

答案 0 :(得分:1)

也许你只想要一个简单的GROUP BY

SELECT to_char(p.date_from, 'Month') as month,
       sum(pl.amount)
FROM hr_payslip p
   INNER JOIN hr_payslip_line pl
      ON (p.id = pl.slip_id AND pl.code = 'NET')
GROUP BY to_char(p.date_from, 'Month');

答案 1 :(得分:0)

我是使用以下查询

完成的
select to_char(to_timestamp (date_part('month', p.date_from)::text, 'MM'), 'Month') as Month, 
sum(pl.amount) from hr_payslip p
INNER JOIN hr_payslip_line pl  on (p.id = pl.slip_id and pl.code = 'NET')  group by month

输出:

enter image description here