-->
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.  [ 7 posts ] 
Author Message
 Post subject: Aggregate functions and NHibernate
PostPosted: Wed Oct 03, 2007 2:00 pm 
Newbie

Joined: Wed Oct 03, 2007 1:53 pm
Posts: 5
Hi there,

currently, I am trying to use aggregate functions with NHibernate. Basically, I want to calculate the average value of a table's column and getting the total number of records of this table at the same time - but both only for records that match a specific critera.

In SQL Server this works:

select avg(Stars), count(*) from Rating where Page = 'Default.aspx'

The result are two columns, the first one with 42, the second one with 23 as value.

Now I am trying to run this query with NHibernate by creating a query and specifying the file name as named parameter. I run this query by calling Enumerable() and get an IEnumerable as result - well, what am I going to do with this?

Casting to object[] fails, although IMHO results with more than one column should be object[], shouldn't they? The exception tells me that casting from Int64 to object[] is not possible.

So, I have tried to run this query as two queries - one for avg, one for count. Since now only one value is returned, I have tried to call UniqueResult to get the scaler value - but it does not work, too.

C#-Code:
double? average = session.CreateQuery("select avg(Stars) from Rating where Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<double?>();
int count = session.CreateQuery("select count(*) from Rating where Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<int>();

NHibernate tells me, that the result is not unique ... in both cases 23 rows are returned (which is the number of rows) - why?

Any idea what this might be, and how I can fix this?

Thanks,


Golo


Top
 Profile  
 
 Post subject: Re: Aggregate functions and NHibernate
PostPosted: Thu Oct 04, 2007 12:34 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Golo wrote:
C#-Code:
double? average = session.CreateQuery("select avg(Stars) from Rating where Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<double?>();
int count = session.CreateQuery("select count(*) from Rating where Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<int>();


This seems strange to me, but all the examples fromthe docs use aliases. Just for kicks, have you tried using one? Like:
Code:
select avg(r.Stars) from Rating r where r.Page = :Page


Seems worth a try.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 1:45 pm 
Newbie

Joined: Wed Oct 03, 2007 1:53 pm
Posts: 5
Hi,

thanks for your hint - it has been worth a lot :-). Now I have:

double? average = session.CreateQuery("select avg(r.Stars) from Rating r where r.Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<double?>();
int count = session.CreateQuery("select count(*) from Rating r where r.Page = :Page").SetString("Page", url.Substring(url.LastIndexOf('/') + 1)).UniqueResult<int>();

and the first one works (although the results always get casted to int, no idea why).

The second one now gives me a "The specified cast is invalid" ... any idea what still might be wrong?

Have a nice day,


Golo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 1:53 pm 
Newbie

Joined: Wed Oct 03, 2007 1:53 pm
Posts: 5
Okay, the question with "count" is answered ... count returns long, not int ... just change the data type to long and it works :-).

So, there is only one question left - why does AVG return an int, not a double?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 2:04 pm 
Newbie

Joined: Wed Oct 03, 2007 1:53 pm
Posts: 5
Okay, this question can also be answered: Stars is an INT column, and SQL Server 2005 returns INT when you calculate AVG on an INT, it returns DOUBLE when you calculate AVG on a DOUBLE column.

Since I do not want to change the column type, how can I modify the query that it returns a double?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 2:08 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Either cast ( "avg( cast( r.Stars as float ) )..." ) or multiply by 1.0 ("avg( r.Stars * 1.0)"). NH might be able to do both.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 2:19 pm 
Newbie

Joined: Wed Oct 03, 2007 1:53 pm
Posts: 5
Great :-)!

You made my day :-)!

The first one results in an error ("syntax error near cast"), but the second one works as expected :-)!

Thanks a lot for your help!


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