在Presto中的开始日期和结束日期之间将日期插入表中 [英] inserting dates into a table between a start and end date in Presto

查看:75
本文介绍了在Presto中的开始日期和结束日期之间将日期插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含开始日期和结束日期之间的所有日期的表格.在不列出插入中的每个日期的情况下,最好的方法是什么?

I want to create a table with all the dates between a start date and end date. What's the best way to do this without listing every date out in the insert?

例如:开始日期是 1/1/2018,结束日期是 1/4/2018 那么结果表应该是:

for example: start date is 1/1/2018 and end date is 1/4/2018 then the resulting table should be:

date
------
1/1/2018
1/2/2018
1/3/2018
1/4/2018

推荐答案

您可以使用 sequence(date, date) 生成范围内的日期:

You can use sequence(date, date) to generate the dates in a range:

presto:default> create table dates as
select x from unnest(sequence(date '2018-01-01', date '2018-01-04')) t(x);
...
presto:default> select * from dates;
     x
------------
 2018-01-01
 2018-01-02
 2018-01-03
 2018-01-04
(4 rows)

注意:sequence 自 Presto 0.197 版起适用于 date.

Note: sequence works with dates since Presto version 0.197.

这篇关于在Presto中的开始日期和结束日期之间将日期插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆