I have the following SQL statement:
SELECT DISTINCT ON (watch.url)
watch.url, watch.name, user_url.frequency, watch.selector, price.date
FROM watch
JOIN user_url on user_url.url = watch.url
LEFT JOIN price on price.url = watch.url
WHERE (1400575234 > price.date + user_url.frequency)
OR price.date is null
ORDER BY watch.url, price.date DESC
Where 1400575234
is the current unix timestamp. The table price holds the price of an item at different time intervals, one per row.
Currently this query will return me all the items because at some point they have had an entry which is less than the current date. Whereas I am only looking at grabbing the URL's whose price.date is less than the current time but I only want to match against the highest date for the url in the price table.
I think I need to run the order by
before the select distinct
works. Does anyone have any ideas?
(Hope this makes sense)