I have a little SQL Distinct puzzle that i cannot solve (or at least not in an very elegant way).
I have two tables (try to ignore the simplicity of the example). I'm using MSSQL 2008 if that makes much of a difference.
Table: Category
| categoryId (uniqueidentifier) PK |
| Name varchar(50) |
Table: Download
| downloadId (uniqueidentifier) PK |
| categoryId (uniqueidentifier) FK |
| url (varchar(max)) |
| createdate (datetime) |
I have a few categories in the Category table and potentially a lot of download URLs in the Download table. I'm interested in selecting the newest using the createdate (or a top 5 if that is possible) download url for each category from the Download table.
Currently I'm doing the following, but that is not very nice, and can hardly be the correct way to do it.
SELECT
categoryId,
max(convert(BINARY(16),downloadId)) as downloadId,
max(createdate) as createdate
INTO tmp
FROM Download
GROUP BY categoryId
ORDER BY createdate
SELECT url
FROM Download
WHERE downloadId IN
(SELECT CONVERT(uniqueidentifier, downloadId) FROM tmp)
DROP Table tmp
Any suggestions would be much appreciated.