【发布时间】:2021-04-24 06:38:56
【问题描述】:
我有以下代码,
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 |
| ... | ... | ... | ... | ... |
| ... | ... | ... | ... | ... |
等等。
no_of_customers_ytd 将在每个新年伊始(1 月 1 日)设置为零。 但我需要在一个特殊的日期将其设置为零,比如每年的 1.June。 所以我需要每年从 1.June 到 30.March 之间的所有事情的总和。
感谢您的帮助。
【问题讨论】:
-
请以文本形式提供表格描述 (DDL) 和示例数据 - 无图像(或 fiddle)以及该数据的预期输出。
标签: sql postgresql date sum