I need to know how I can rewrite the following SQL query in NHibernate's ICriteria format. It is basically a way to mimic the MS-SQL's RANK() feature and return only those results that are most current.
SELECT a.Name, a.Value, a.CreationDate
FROM MyTable a
WHERE EXISTS
(
SELECT NULL
FROM
(
SELECT TOP 1 CreationDate
FROM MyTable
WHERE Name = a.Name
ORDER BY CreationDate DESC
) b
WHERE b.CreationDate = a.CreationDate
)
For example, given a table with the following data:
NAME, VALUE, CREATIONDATE
'Key One', 'value one v1', '2009-11-11'
'Key One', 'value one v2', '2009-11-12'
'Key Two', 'value two v1', '2009-11-09'
'Key Three', 'value three v2', '2009-09-09'
'Key Three', 'value three v1', '2009-09-06'
'Key Three', 'value three v3', '2009-10-01'
The results of the above query would be:
'Key One', 'value one v2', '2009-11-12'
'Key Two', 'value two v1', '2009-11-09'
'Key Three', 'value three v3, '2009-10-01'