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: How to return Rank and Percentile information?
PostPosted: Thu Aug 24, 2006 11:05 am 
Newbie

Joined: Tue Jul 11, 2006 11:14 am
Posts: 2
Everything works great until I need to return the "Rank" and "Percentile" information for a given record. It can not be expressed using my object-oriented model. I have two problems:
1. I need to execute native sql query.
2. I need to map the query result back to an object. And the mapping is not based a physical table column

Please shed a light on me. Thank you in advance!

Build 1.2.0.Alpha1

The table is:
create table dbo.tblGamePlayHistory (
pk_gamePlayHistoryId int identity(1,1) not null,
Role nvarchar(100),
QuizScore float,
CaseScore float
)


The query I need to execute is:
Code:
select Rank, (Rank*100)/(RecordCount+1) as Percentile, pk_gamePlayHistoryId, QuizScore, CaseScore from
(
   select
      (select count(g2.pk_gamePlayHistoryId)
         from tblGamePlayHistory g2
         where (g2.QuizScore + g2.CaseScore) > (g1.QuizScore + g1.CaseScore)
            
      ) +1 as Rank,
      (select count(g3.pk_gamePlayHistoryId) from tblGamePlayHistory g3 where g3.QuizScore is not null and g3.QuizScore is not null) as recordCount,
      g1.pk_gamePlayHistoryId,
      g1.QuizScore,
      g1.CaseScore   
   from tblGamePlayHistory g1
   where g1.QuizScore is not null and g1.QuizScore is not null
) t
where t.pk_gamePlayHistoryId = 10
order by Rank


Top
 Profile  
 
 Post subject: Re: How to return Rank and Percentile information?
PostPosted: Fri Aug 25, 2006 3:59 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
zhangyi wrote:
The query I need to execute is:


Not really an answer, but the first thing that comes to my mind is: why put so much logic into one intricate query? Maybe there is a reason for it (performance?), but I would tend to let a class model do the job and keep your queries simple and generic.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 4:38 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
May I recommend you the (N)Hibernate docs? Look for native queries. Also you can have an object constructed from your query, again look in the docs.

cheers,
radu


Top
 Profile  
 
 Post subject: Re-state the problem I need to solve.
PostPosted: Fri Aug 25, 2006 10:41 am 
Newbie

Joined: Tue Jul 11, 2006 11:14 am
Posts: 2
From my past experience, I don't think Ojbect Model can handle all the issues effectively. I think "Finding Rank for a record" is something database can handle more efficiently than Object Model.

I did read the nhibernate manual. I didn't find answer there. NHibernate requires to use Class name in native sql. I totall agree with that decision since that hides the actual table name in the native sql. What I look for is how to map the result (select rank, percentile from ....) back to the RankMapper class. As you can see, Rank is a calculated column. It does'n physically exist in any table. So does Percentile in class RankMapper.

public class RankMapper : MapperBase<int>
{
public RankMapper(int rank, int percentile)
{
_rank = rank;
_percentile = percentile;
}

private int _rank;
public virtual int Rank
{
get { return _rank; }
set { _rank = value; }
}

private int _percentile;
public virtual int Percentile
{
get { return _percentile; }
set { _percentile = value; }
}
}

The solution for now is that NHibernate execute the real-native-sql(use table name) and return a IDataReader. I am good for now. But I am still looking for a solution in a NHibernate way.

I just wonder how you guys handle the "find the rank for a record" type of problem using NHibernate. Think about just 8000 record in db.


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.