SQL查询按月/年汇总金额 [英] SQl query to sum-up amounts by month/year

查看:72
本文介绍了SQL查询按月/年汇总金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的表TABLE":

I have a table "TABLE" like this:

Date(datatime)
Paid(int)

我每个月有多个付费"金额.我想总结一下每月/每年的支付金额.

I have multiple "Paid" amounts per month. I would like to sum up the Paid amount per month/year.

到目前为止,这是我尝试过的,但我在 EXTRACT 和 MONTH 中遇到了错误,但是这些年来我还远远没有完成.

So far this is what I tried but I get errors in EXTRACT and in MONTH and however I am far to get it done with the years.

SELECT 
     EXTRACT(MONTH FROM Period) AS reference_month
     , SUM(Paid) AS monthly_payments 
FROM Paid 
GROUP BY EXTRACT(MONTH FROM Period) 
ORDER BY EXTRACT(MONTH FROM Period)

我不是很擅长这个,我真的很感激一些帮助.

I am not really handy at this and I would really appreciate some help.

推荐答案

select year(date) as y, month(date) as m, sum(paid) as p
from table
group by year(date), month(date)

这篇关于SQL查询按月/年汇总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆