Getting the "latest" related record from a SQL DB

Gianni Ceccarelli dakkar at thenautilus.net
Thu Oct 9 17:48:35 BST 2014


I think you want a window function (see for example
http://www.postgresql.org/docs/9.1/static/tutorial-window.html )

Something like:

  SELECT name,title
  FROM (
   SELECT artist.name,album.title,rank() OVER (
     PARTITION BY artist.id ORDER BY album.date DESC
   ) as r
   FROM artist
   JOIN album ON artist.id = album.artist_id
  ) x
  where r=1;

(tested in Postgres)

-- 
	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88

Abracadaver: Now you meet your doom!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
Url : http://london.pm.org/pipermail/london.pm/attachments/20141009/072cab9c/signature.pgp


More information about the london.pm mailing list