Postgres按月选择总和

时间:2018-01-29 19:49:54

标签: postgresql select

我有表格证据:

id      id_customer     id_service     amount       date 
1           1                1          50.0        2018-01-10
2           2                1          15.7        2018-01-18
3           3                2          14.9        2018-02-07
4           1                3          700.0       2018-02-28
5           2                1          250.0       2018-03-19
6           3                2          315.7       2018-03-08
7           1                1          100.0       2018-04-28
8           2                2          132.0       2018-04-26

我想在2018年的月份之前制作SUM并输出如下:

  amount  month
    65.7     1
    714.9    2
    565.7    3
    232.0    4

这是我想要帮助的选择。

select sum(amount),extract(month from date) from evidence where extract(year from date)=2018 and amount>0 group by date;

1 个答案:

答案 0 :(得分:0)

喜欢这个

SELECT SUM(amount) AS amount , date_trunc('month', date) as month
FROM evidence 
WHERE extract(year from date)=2018
GROUP BY month