【发布时间】:2019-06-15 06:40:42
【问题描述】:
我有一个包含 3 列的表 - start、end 和 emp_num。我想生成一个新表,其中包含每个员工的这些日期之间的所有日期。需要使用 Presto。
我参考了这个链接 - inserting dates into a table between a start and end date in Presto
尝试通过创建序列来使用 unnest 函数,但是,我不知道如何通过从另一个表的两列中提取日期来创建序列。
select unnest(seq) as t(days)
from (select sequence(start, end, interval '1' day) as seq
from table1)
这是表格和预期格式
Table 1:
start | end | emp_num
2018/01/01 | 2018/01/05 | 1
2019/02/01 | 2019/02/05 | 2
Expected:
start | emp_num
2018/01/01 | 1
2018/01/02 | 1
2018/01/03 | 1
2018/01/04 | 1
2018/01/05 | 1
2019/02/01 | 2
2019/01/02 | 2
2019/02/03 | 2
2019/02/04 | 2
2019/02/05 | 2
【问题讨论】:
-
创建一个包含所有日期的新表。假设表名为“日历”,列名为“mydate”,您可以执行
SELECT calendar.mydate, emp_num FROM table1 INNER JOIN calendar ON calendar.mydate BETWEEn table1.start and table1.end; -
@JNevill:我很难创建日历表。我的原始数据有 150 多年的历史。你能告诉我如何创建这个日历表吗?我事先不知道开始和结束日期,这些是以编程方式生成并填充到 table1
-
Check out this answer which generates sequences of dates on the fly。那只是在快速搜索之后,所以我敢打赌 prestodb 会有更多技术。
-
@JNevill:谢谢。我之前提到过该链接,但似乎无法正确获得 presto 等价物(特别是对于 dateadd 函数),这就是我发布问题的原因。感谢您的回复
-
明白了。我希望我有一个 prestodb 盒子可以放屁,看看我能不能给你一个更好的答案。仅仅在excel中列出日期并导入只是为了设置日历表可能是值得的。它们在任何数据库中都非常有用。