在postgresql中进行4周或会计月份分组

时间:2017-06-07 12:48:21

标签: postgresql group-by

我要求将事务表中的数据分组为4周组或会计月分组,以及有关如何在postgresql上处理它的任何建议

此致

2 个答案:

答案 0 :(得分:0)

使用date_trunc https://www.postgresql.org/docs/9.1/static/functions-datetime.html

除此之外的任何内容都需要更多信息。复制一些例子以及你已经尝试过的任何事情。

SELECT date_trunc('week', your_date_field)
FROM your_table_name
GROUP BY 1

答案 1 :(得分:0)

SELECT sum(ITEM_C._demand) AS aggre_demand,ITEM_C._week as account_month  FROM (SELECT  sum(quantity) AS _demand,( CASE WHEN floor((extract(doy from shipment_date)-1)/7)+1 > 0 AND floor((extract(doy from shipment_date)-1)/7)+1 <=4 THEN 1 
ELSE (CASE WHEN floor((extract(doy from shipment_date)-1)/7)+1 >4 AND floor((extract(doy from shipment_date)-1)/7)+1 <=8 THEN 2 ELSE 3 END ) END)AS _week, date_trunc('year',shipment_date) AS _Year
FROM smp.shipment
GROUP BY floor((extract(doy from shipment_date)-1)/7)+1,date_trunc('year',shipment_date)) AS ITEM_C
GROUP BY ITEM_C._week, ITEM_C._Year

我认为这不是空闲的方式,因为我需要重复这个1..52周总计13次