-->
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.  [ 4 posts ] 
Author Message
 Post subject: CreateSQLQuery using Aggregate and Sub Table in FROM Clause
PostPosted: Mon Apr 21, 2008 9:09 am 
Newbie

Joined: Mon Apr 21, 2008 8:49 am
Posts: 2
Hi

I am trying to run some native SQL using CreateSQLQuery. The issue here is that I am performing an aggregate function on the sub table (in the FROM clause) that is also doing an aggregate.

Here is the SQL I am trying to run:
Code:
SELECT AVG(sub.profileCount)
FROM (SELECT COUNT(pv.ProfileId) AS profileCount
          FROM ProfileView pv
          GROUP BY pv.ProfileId) AS sub


This is doing a COUNT on ProfileID of ProfileView and then doing an AVG on that result.

When I use:
Code:
IQuery qry =  ActiveSession.CreateSQLQuery(sql);
int i = Int32.Parse(qry.UniqueResult<double>().ToString());

I get the following error:
Code:
Return types of SQL query were not specified [*MY SQL STATEMENT*]


Using
Code:
int i = Int32.Parse(qry.List()[0].ToString());

Gives the same error. So I thought of doing a .AddEntity(typeof(double)), but errors as well with the following:
Code:
No persister for: System.Double


So, does anybody have an idea on how to get the result?

Cheers :)

Edit: I am using SQL 2005


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 2:06 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
You have to add a scalar:

query.AddScalar("alias", NHibernateUtil.Double);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 4:20 am 
Newbie

Joined: Mon Apr 21, 2008 8:49 am
Posts: 2
Thanks - that sorted it :)

I'm guessing that if I have more than one return field I just add an additional .AddScalar for each one?

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 7:17 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Right, you need an AddScalar() for each column in the SELECT clause.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.