If you are using an RDBMS that has tuple-capable IN like Postgresql, MySql, and Oracle. You can use the following approach.
So, let's say you already have an existing view:
create view page_latest as SELECT page_url, MAX(last_modified) recentDate FROM tbl GROUP BY page_url; select * from page_latest;
Output...
| PAGE_URL | RECENTDATE | --------------------------------------------- | abc.com | October, 01 2010 10:00:00+0000 | | xyz.com | October, 04 2010 12:10:00+0000 |
... based on this data:
| PAGE_URL | LAST_MODIFIED | MESSAGE | ------------------------------------------------------------------ | abc.com | October, 01 2010 10:00:00+0000 | no alarm | | xyz.com | October, 04 2010 12:10:00+0000 | no surprises | | xyz.com | October, 04 2010 12:00:00+0000 | fake plastic trees | | xyz.com | October, 04 2010 10:00:00+0000 | creep | | abc.com | October, 01 2010 08:00:00+0000 | thom yorke |
You can find all the latest posts by reusing the above view using IN, which is part of the above RDBMSes: http://www.sqlfiddle.com/#!2/b8193/2
select * from tbl where (page_url,last_modified) in (select page_url, recentDate from page_latest);
Output:
| PAGE_URL | LAST_MODIFIED | MESSAGE | ------------------------------------------------------------ | abc.com | October, 01 2010 10:00:00+0000 | no alarm | | xyz.com | October, 04 2010 12:10:00+0000 | no surprises |
Not only the request is shorter, the easier it is to read. Although, if you donโt have a look, you can insert a โrequestโ in your request: http://www.sqlfiddle.com/#!2/b8193/5
select * from tbl where (page_url,last_modified) in (SELECT page_url, MAX(last_modified) recentDate FROM tbl GROUP BY page_url);
This answer does not work on the Sql server. There are only a few databases that do not support tuple-capable IN , unfortunately Sql Server is one of them.