Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrestoDB: select all dates between two dates

Tags:

sql

presto

I need to form a report which provides some information per each date within dates interval.

I need to have it within a single query (can't create any functions or supporting tables).

How can I achieve that in PrestoDB?

Note: There are lots of vendor specific solution here, here and even here. But none of them satisfies my need as they either don't work in Presto or use tables/functions.

To be more precise here is an example of query:

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
like image 597
Sasha Shpota Avatar asked Jul 18 '18 11:07

Sasha Shpota


People also ask

How do I generate all dates between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

How can I get date between two dates in SQL query?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference.

How do you select a date range?

In the calendar, click the desired start date, then click the end date. The selected days are highlighted. OR. Enter start and end dates in the Date Range fields.


1 Answers

You can use the Presto SEQUENCE() function to generate a sequence of days as an array, and then use UNNEST to explode that array as a result set.

Something like this should work for you:

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)
like image 129
Ike Walker Avatar answered Nov 07 '22 10:11

Ike Walker