Skip to content
Advertisement

Calculate the difference of dates in one column

DB-Fiddle

Expected result:

I want to calculate the date_difference between the MAX and the MIN order_date per campaign.

So far I was able to come up with this query:

This solution would work if the dates would be in two separate columns.

How do I have to modify the query to also make it work if they are in one column?

Advertisement

Answer

date_part(day, ...) returns the day of the month, not the number of days for that date. If you want the difference in days, just subtract the two dates. As you want the min() and max() dates, you will have to group by the campaign to do that.

Online example

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement