r' s peiling_date等效于sql
我想在SQL(PostgreSQL)中实现R的ceiling_date fucntion。
因此,我每天都有一个日期,每天都有相应的销售,我想在一个日期(例如星期五)积累一个星期的销售。
输入格式:
黄色的日期是按预期输出格式
任何帮助将不胜感激。谢谢
I want to implement R's ceiling_date fucntion in SQL (Postgresql).
So I have dates in a column for everyday with corresponding sales and I want to accumulate the sales for a week over a single date (say Friday).
Input Format:
Dates in yellow are the dates to aggregate sales on
Expected output format:
This can easily be done in R using ceiling_date but I want to do it in SQL itself.
Any help would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接受和处理 iso 8601 标准是处理日期范围的最简单的标准。但是,这实质上是一个标准定义,这本质上是:
date_trunc 功能一周中的一天。
非ISO 8601的日期/周处理提出了一些棘手的过程,以获取适当的周定义。以下是星期五至周四的一周定义。它创建a 日期范围从桌子上的第一个星期五开始的一年,然后使用该范围包含操作员加入以确定适当的求和期,
请参见两个。
注意:演示更改样本日期,从1月(2022-01-01 ...)到5月(2022-05-01 ...),因为2022年1月6日是星期四,星期五不是描述,但20122年6月6日是5月6日。星期五。同样,结束6-MAY的值的总和为38(不是42个)。最后,都没有查询尝试限制日期,但是通过数据结束进行处理。要么可以解决多年的数据。
Accepting and processing the ISO 8601 Standard is by far the easiest for processing date ranges. But this imposes a standard definition, which is essentially:
The date_trunc function gives the first date of the week, adding 6 gives the last day of the week.
Date/Week processing for non ISO 8601 presents somewhat tricky process to get the appropriate week definition. The following does so for week Friday - Thursday definition. It creates a date range for a year beginning with the first Friday in the table, then joins using the range contains operator to determine the appropriate summation period
See demo of both here.
NOTE: Demo changes sample date from January (2022-01-01 ...) to May (2022-05-01 ...) as 6-January-2022 was Thursday not Friday as description, 6-May-2022 is however Friday. Also the sum of values ending 6-May is 38 (not 42 as indicated). Finally, neither query attempts a limiting date, but processed through end-of-data. Nor does either address multiple years of data.
” 2022-大1月20日,有3个星期五:'2022-01-07','2022-01-14','2022-01-21'。
我们需要按照这3个星期五的订单按销售日期进行分区。
现在,问题是要计算所有这些日期属于这3个星期五。
最终代码:
处理代码:
demo
idea: for 2022-Janurary-1 to 2022-Janurary-20, there is 3 Fridays:'2022-01-07','2022-01-14', '2022-01-21'.
We need to partition by these 3 friday order by sales date.
Now the problem is now to compute get all these date belong to these 3 fridays.
final code:
processing code: