-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help with HQL (Self join ?)
PostPosted: Wed Feb 11, 2009 8:50 am 
Newbie

Joined: Fri Nov 14, 2003 12:09 pm
Posts: 6
Location: Paris
I have a table:
Employee Salary

Can somebody help me to express the following HQL request:
Find employees and for every employee the count of employees which have a salary superior to this employee ?

I made some searches but I do not manage to express this request.

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 9:14 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I think this is fine:
select e.id, count(*) from Employee e, Employee e1 where e.salary < e1.salary and e.id <> e1.id group by e.id

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Thanks it works
PostPosted: Wed Feb 11, 2009 12:17 pm 
Newbie

Joined: Fri Nov 14, 2003 12:09 pm
Posts: 6
Location: Paris
If I have four Employees:
E1 1000
E2 2000
E3 3000
E4 4000

The query:
select e1, count(*) from Employe as e1, Employe as e2 where e1.salaire > e2.salaire group by e1
gives only:
E2 2000 1
E3 3000 2
E4 4000 3

But the query
select e1, count(*) from Employe as e1, Employe as e2 where e1.salaire >= e2.salaire group by e1
gives:
E1 1000 1
E2 2000 2
E3 3000 3
E4 4000 4
And that is really what I wanted (the rank of the Employee for the salary)

Thanks for your help


Top
 Profile  
 
 Post subject: Doesnt work with Derby
PostPosted: Wed Feb 11, 2009 12:22 pm 
Newbie

Joined: Fri Nov 14, 2003 12:09 pm
Posts: 6
Location: Paris
I forget to mention that those queries work with MySQL but not with Derby, (probably group by not allowed with count)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 2:22 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
See this bug report:
http://opensource.atlassian.com/project ... e/HHH-1615

The workaround is this HQL:
Code:
select e1 , count(*) from Employee as e1, Employee as e2 where e1.salary >= e2.salary group by e1.id, e1.fname, e1.lname, e1.salary, e1.department

In the group by clause list all the properties of the Employee entity.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: The real query !
PostPosted: Thu Feb 12, 2009 5:34 am 
Newbie

Joined: Fri Nov 14, 2003 12:09 pm
Posts: 6
Location: Paris
I had taken the example of Employee to explain my problem. Here is my real request:

select new galland.planning.backing.SeanceInfo(s1, count(*))
from Seance as s1 , Seance as s2
where s1.jour >= :from
and s1.jour <= :to
and s1.enseignant = :enseignant
and s1.enseignement = s2.enseignement
and (s1.jour > s2.jour or s1.jour = s2.jour and s1.heure >= s2.heure)
group by s1
order by s1.jour, s1.heure")
.setDate("from", from)
.setDate("to", to)
.setEntity("enseignant",getEnseignant())
.list();

It is for a university schedule and It finds the sessions (Seance) between two dates from and to for a givent teacher (Enseignant) with for every session the rank (by day and hour) in the course (enseignement).
It works, Thanks to HQL !

Thanks again for your help
Dominique


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