How to get date_diff from previous rows in presto?

by elisha_langworth , in category: MySQL , 7 days ago

How to get date_diff from previous rows in presto?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 6 days ago

@elisha_langworth 

In Presto, you can use a combination of window functions and the lag function to calculate the difference between dates in previous rows. Here's an example query that demonstrates how to achieve this:

1
2
3
4
5
6
SELECT 
  event_date,
  LAG(event_date) OVER (ORDER BY event_date) AS previous_event_date,
  DATEDIFF(event_date, LAG(event_date) OVER (ORDER BY event_date)) AS date_diff_in_days
FROM 
  your_table_name


In this query:

  • LAG(event_date) OVER (ORDER BY event_date) retrieves the event date from the previous row.
  • DATEDIFF(event_date, LAG(event_date) OVER (ORDER BY event_date)) calculates the difference in days between the current event date and the previous event date.
  • your_table_name is the name of the table where your data is stored.


You can modify the query as needed to fit your specific requirements.