我有以下代码,
SELECT
years_month_count.day_date,
years_month_count.year_date,
years_month_count.month_date,
years_month_count.no_of_customers_day,
sum(years_month_count.no_of_customers_day) OVER (PARTITION BY year_date ORDER BY day_date) AS no_of_customers_ytd
FROM (
SELECT
DATE(date) as day_date,
DATE_PART('year',date) as year_date,
DATE_PART('month',date) as month_date,
count(prepare_first_buyer.person_id) as no_of_customers_day
FROM (
SELECT
DATE(bestelldatum),
person_id,
ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id)
FROM ani.bestellung
) prepare_first_buyer
WHERE row_number=1
GROUP BY DATE(date), DATE_PART('year',date),DATE_PART('month',date)
ORDER BY DATE(date), DATE_PART('year',date),DATE_PART('month',date)
) years_month_count
输出看起来像这样:
day_date | year_date | month_date | no_of_customers_day | no_of_Customers_ytd |
---|---|---|---|---|
2017-04-04 | 2017年 | 4 | 6 | 6 |
2017-04-05 | 2017年 | 4 | 4 | 10 |
... | ... | ... | ... | ... |
... | ... | ... | ... | ... |
等等。
在每个新年(1月1日)开始时,no_of_customers_ytd都将设置为零。但是我需要在特殊的日期将其设置为零,例如每年1月1日。因此,我每年需要从6月1日到3月30日之间的所有款项。
谢谢。
SELECT
*,
SUM(value) OVER (PARTITION BY -- 4
date_part('year', -- 3
the_date - interval '5 months' -- 2
)
)
FROM t
WHERE date_part('month', the_date)::int NOT BETWEEN 4 AND 5 -- 1
4
和5
year-06-01
移至year-01-01
,因此您需要减去5 months
。由于您的日期范围永远不会超过一年,因此所有相关数据现在都具有同一年份,这是一个很好的组准则year
零件以将其用作组/分区标准本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句