PostgreSQL每月每十五天选择一次

PostgreSQL每月每十五天选择一次,sql,postgresql-9.1,Sql,Postgresql 9.1,你好,我有一些记录: date Money 1/6/2014 100 12/6/2014 2200 13/6/2014 500 1/3/2014 100 2/5/2014 2200 30/5/2014 500 30/6/2014 100 23/6/2014 2200 31/6/2014

你好,我有一些记录:

date               Money

1/6/2014            100
12/6/2014            2200
13/6/2014            500
1/3/2014            100
2/5/2014            2200
30/5/2014            500
30/6/2014            100
23/6/2014            2200
31/6/2014            500
嗯,我有一个参数,比如它的日期,我需要7月30日的所有记录,但我需要记录的总和,如果很容易的话

select sum(money) from info where date <= param
group by month
我希望得到的结果是:

15/6/2014   sum(money)
15/5/2014   sum(money)
15/4/2014   sum(money)

我需要每月15天的记录,问题是如何安排到一个月内15日之前的天数,从16日起的天数在下个月。你可以减去15天。这意味着上个月的1-15天。然后,加上一个月。以下是一种在Postgres中使用的方法:

select to_char(date - 15 * interval '1 day' + interval '1 month', 'YYYY-MM') as mon,
       sum(money)
from info
where date <= param
group by to_char(date - 15 * interval '1 day' + interval '1 month', 'YYYY-MM')
order by 1;
选择将字符(日期-15*间隔“1天”+间隔“1个月”、“YYYY-MM”)作为周一,
金额(货币)
来自信息

基本上你想按“会计月”求和,15号是你的截止日期?所以你的
2014年6月15日
实际上会涵盖日期
2014年5月16日->2014年6月15日
?是的,但不是怎么做戈登谢谢,但在查询中我看到你将我的所有数据置于参数下至少15天(2014年6月30日-每月15天),但如果我在这种情况下输入2014年6月15日不应该减去15天,对吗?,我的问题是,是否要使总和减去所选的而不是在何处?我看到查询的结果在第6个月总是取30的较低结果,谢谢你的帮助@阿尔贝托。我不明白这个评论。也许您可以通过编辑问题并添加具有所需结果的示例数据来说明您的意思。对不起,在sql查询结果中,总是在2014年6月30日之前获取,查询中唯一一个名为mon的字段每15个月都有正确的日期,但查询总是在30、29、28日获取金额的总和。。。2014年6月15日我的问题是如何将查询条件设置为只添加小于或等于每月15日的日期,谢谢!感谢Gordon的帮助,但我需要为全天组选择,并为“财政月”选择记录。在这种情况下,每15日,我需要对我的记录进行分组,例如:2014年6月15日,下一行记录的金额为:2014年5月15日,下一行记录的金额为:2014年4月15日,我需要从表中选择数据,其中每个月15日(会计月)@Alberto。这就是我最初的回答。
select to_char(date - 15 * interval '1 day' + interval '1 month', 'YYYY-MM') as mon,
       sum(money)
from info
where date <= param
group by to_char(date - 15 * interval '1 day' + interval '1 month', 'YYYY-MM')
order by 1;
select to_char(date, 'YYYY-MM') as mon,
       sum(money)
from info
where extract(day from date) <= param
group by to_char(date, 'YYYY-MM')
order by 1;
select sum(money)
from info
where to_char(date, 'YYYY-MM') = to_char(param, 'YYYY-MM') and
      date <= param;