-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Group By in Criteria query
PostPosted: Mon Jun 11, 2007 10:13 am 
Newbie

Joined: Sat Mar 10, 2007 12:46 pm
Posts: 5
I want the latest exports successful export for each of several given export groups.

The SQL is like this:

Select *
From ExportFile res
Inner join
(
Select max(ExportFileID)
From ExportFile ex
Where ex.Status = 1
Group By ExportFileGroupID
) latestFiles ON latestFiles.ExportFileId = res.ExportFileId
Where res.ExportFileGroupID in (@groupsToGet)
(sql has not been syntax checked)

We want to write this as a Criteria query, and we have tried a lot of different combinations with no luck.

Can anyone point us in the right direction?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 12:38 pm 
Newbie

Joined: Mon Jun 11, 2007 6:08 am
Posts: 3
Hi tindbaek,

NHibernate reference document under section 12.6 Projections, aggregation, grouping and 12.7 Detached Queries and Subqueries may be the area to look for. The combination of 12.6 & 12.7 should work for you.

Extract from the reference document page 110:

Code:
DetachedCriteria avgWeightForSex = DetachedCriteria.For(typeof(Cat), "cat2")
.SetProjection( Projections.Avg("Weight") )
.Add( Expression.EqProperty("cat2.Sex", "cat.Sex") );
session.CreateCriteria(typeof(Cat), "cat")
.Add( Subqueries.Gt("weight", avgWeightForSex) )
.List();


I don't know if this is correct but can be something on these lines:

Code:
DetachedCriteria latestFile = DetachedCriteria.For(typeof(ExportFile), "ex")
.SetProjection( Projections.Max("ExportFileID") )
.Add( Expression.Eq("ex.Status", 1)
.Add( Expression.EqProperty("ex.ExportFileId ", "res.ExportFileId ") )
.Add( Projections.GroupProperty("ExportFileId"));

session.CreateCriteria(typeof(ExportFile), "res")
.Add( Subqueries.Eq("ExportFileId ", latestFile ) )
.Add( Expression.In( "ExportFileGroupID", groupsToGet) )
.List();


Here the groupsToGet needs to be an object[] of IDs.


Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.