我有以下SQL语句:
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
其中1400575234
是当前的unix时间戳。表格价格以不同的时间间隔保存商品的价格,每行一个。
目前此查询将返回所有项目,因为在某些时候他们有一个小于当前日期的条目。虽然我只是想抓住其price.date小于当前时间的URL,但我只想匹配价格表中url的最高日期。
我想我需要在order by
工作之前运行select distinct
。有没有人有任何想法?
(希望这是有道理的)
答案 0 :(得分:0)
您可以尝试使用窗口功能:
SELECT url, name, frequency, selector, date
FROM (
SELECT watch.url, watch.name, user_url.frequency, watch.selector, price.date,
row_number() over (partition by watch.url order by price.date desc) as rn
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
) t
WHERE rn = 1
ORDER BY url, date;
顺便说一下:date
是列的可怕名称
答案 1 :(得分:0)
我似乎使用以下
来解决它SELECT DISTINCT ON (watch.url)
watch.url, watch.name, user_url.frequency, watch.selector, p.date
FROM watch
JOIN user_url on user_url.url = watch.url
LEFT JOIN (
SELECT max(price.date) as date, url FROM price GROUP BY url
) as p ON p.url = watch.url
WHERE 1400575234 > p.date + user_url.frequency
ORDER BY watch.url
感谢大家的帮助。如果你能看到更有效的查询请告诉我。