My question is subtly different to other questions I have seen here on StackOverflow, so please do not confuse this with previous answers.
I have a query
SELECT tab1.Id
FROM TABLE1 tab1
WHERE tab1.modified_date between to_date('01/04/2013 10:00:00', 'dd/mm/yyyy HH:MI:SS') and to_date('01/04/2013 11:15:00', 'dd/mm/yyyy HH:MI:SS')
GROUP BY tab1.Id
ORDER BY max(tab1.received_date) desc
I would like to select the TOP 500 rows, and because Oracle processes a where clause before the GROUP and ORDER BY conditions (evidence), I must achieve this by wrapping this query in another to select the top 500.
So, now my query looks like...
SELECT *
FROM (SELECT tab1.Id
FROM TABLE1 tab1
WHERE tab1.modified_date between to_date('01/04/2013 10:00:00', 'dd/mm/yyyy HH:MI:SS') and to_date('01/04/2013 11:15:00', 'dd/mm/yyyy HH:MI:SS')
GROUP BY tab1.Id
ORDER BY max(tab1.received_date) desc ) sub
WHERE ROWNUM <=500
This all works great, but I am struggling to translate this into an NHibernate query using the QueryOver API. The inner query is simple, e.g.
var subquery = QueryOver.Of<Table1>()
.Select(Projections.Group<Table1>(e => e.Id))
.....where clause
.OrderBy(Projections.Max<Table1>(e => e.Received_Date)).Desc
but how do i wrap this in a select * from subquery where rownum <= 500?