I have a database with following structure
url update_time dns_time
-------------------------------
url1 2013-04-05 08:03:23 0.897
url2 2013-09-03 08:03:45 0.765
url1 2013-08-23 09:23:34 2.457
url3 2013-08-34 09:45:47 1.456
//and so on
Now I want to retrieve only latest records of each url. How to achieve this using PostgreSQL select query.
I tried using
select url,
update_time,
dns_time
from dns_lookup_table
where url in('url1','url2','url3')
order by desc limit 1
But it is giving me the url3 latest value that is last record. I tried with desc limit 3 for getting latest values of all 3 urls. I want to retrieve the latest records of url1, url2, url3. Only latest records. And the table dns_lookup_table has records that comes into it dynamically. Sometimes the url record can not be inserted if not available. So order is missing. So I think it is not possible with desc limit.
解决方案SELECT *
FROM dns_lookup_table lut
WHERE NOT EXISTS (
SELECT *
FROM dns_lookup_table nx
WHERE nx.url = lut.url
AND nx.update_time > lut.update_time
);