如何在雪花中的两个日期之间动态生成日期?
我一直在搜索雪花中的一个很好的generate_series类似物,但是到目前为止,我发现的范围有些限制。我见过的大多数示例都使用 rowcount
,但是我需要比这更动态的东西。
我有这些列: Location_ID,subscription_id,start_date,end_date
日期列的日期通常是一年,但是在很多情况下,我需要考虑这一点。
如何在开始日期和结束日期之间产生无间隙的日期范围?
谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以解决此问题,但这是我使用 sql Generator function
datespine_groups
。我之所以喜欢这样做,是因为它的灵活性足够灵活,我可以在日期之间添加每周,每小时或每月的间隔并重复使用代码。
参数
组界限
更改连接以微妙的方式发生的方式,使您可以控制日期如何被过滤出来:global
local - 每个location_id,subscription_id subscription_id都有自己的启动/结束日期,该日期基于日期列中的第一个和最后一个值
混合混合 - 每个位置_id,subscription_id都有自己开始/结束日期,但是它们都共享相同的结束日期,
而不是尝试使其在1查询中完美,我认为使用
混合
进行生成可能更容易,然后过滤> group_start_date
发生在原始数据的end_date
之后。这是SQL。一开始您可以(1)找到一种动态生成3个参数的方法,或(2)硬码一个荒谬的范围,可以持续您的职业生涯,并让其余的查询过滤掉:)
您可以更改
月
到另一个datePart
,我只认为您正在寻找每月。There are several ways to approach this, but here's the way I do it with SQL Generator function
Datespine_Groups
.The reason I like to do it this way, is because its flexible enough that I can add weekly, hourly, or monthly intervals between the dates and reuse the code.
The parameter
group bounds
changes the way the join happens in a subtle way that allows you to control how the dates get filtered out:global - every location_id, subscription_id combination will start on the same start_date
local - every location_id, subscription_id has their own start/end dates based on the first and last values in the date column
mixed - every location_id, subscription_id has their own start/end dates, but they all share the same end date
Rather than try and make it perfect in 1 query, I think it's probably easier to generate it with
mixed
and then filter out where thegroup_start_date
occurs after theend_date
of your original data.Here's the SQL. At the very beginning you can either (1) find a way to dynamically generate the 3 parameters, or (2) hard code a ridiculous range that'll last your career and let the rest of the query filter them out :)
You can change
month
to anotherdatepart
, I only assumed you were looking for monthly.