PrestoDB:select 两个日期之间的所有日期

PrestoDB: select all dates between two dates

我需要制作一份报告,提供日期间隔内每个日期的一些信息。

我需要在单个查询中使用它(不能创建任何函数或支持表)。

如何在 PrestoDB 中实现这一点?

注意: 有很多供应商特定的解决方案 here, here and even here。但是 none 满足了我的需要,因为它们要么不能在 Presto 中工作,要么使用 tables/functions.

更准确地说,这里是一个查询示例:

WITH ( query to select all dates between 2017.01.01 and 2018.01.01 ) AS dates
SELECT 
  date     date, 
  count(*) number_of_orders
FROM dates dates
LEFT JOIN order order
  ON order.created_at = dates.date

您可以使用 Presto SEQUENCE() function to generate a sequence of days as an array, and then use UNNEST 分解该数组作为结果集。

像这样的东西应该适合你:

SELECT date_array AS DAY
FROM UNNEST(
      SEQUENCE(
        cast('2017-01-01' AS date), 
        cast('2018-01-01' AS date), 
        INTERVAL '1' DAY
      ) 
    ) AS t1(date_array)