-->
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: Stuck on list query - DISTINCT doesn't work?
PostPosted: Sun Sep 14, 2003 4:20 pm 
Newbie

Joined: Tue Sep 02, 2003 8:19 pm
Posts: 19
I'm currently trying the following query:

SELECT pg.name, pg.id
FROM PollGroup pg, Poll p
WHERE
p IN elements(pg.polls)
AND (
pg.owner = :usr
OR :usr IN elements(pg.pollCreators)
OR p.owner = :usr
OR :usr IN elements(p.editors)
)
ORDER BY pg.name

A PollGroup has a set of Polls. Every Poll and PollGroup has one single User in the property "owner". A PollGroup has a set of users as "pollCreators", a Poll has a similar set named "editors".

I'm trying to select the name and id of every poll group that a User is associated with, either as owner, pollCreator or editor. The above query seems to work, but I'm getting double entries for the case where a user owns both a PollGroup and a contained Poll. How can I work around that? I tried using "SELECT DISTINCT pg.name, pd.id ...", but that gave me a QueryException about expecting an aggregate function.

Any idea on how I can make this query give me only distinct entries? It's probably an easy solution somewhere, but I'm really stuck at the moment.

Thanks
Carl-Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2003 10:03 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
you could try using the "group by" syntax


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2003 10:58 pm 
Newbie

Joined: Tue Sep 02, 2003 8:19 pm
Posts: 19
> you could try using the "group by" syntax

Thanks - I tried adding GROUP BY pg.id to the above query, but then I get the following JDBC error through:

Could not execute query: ERROR: Attribute pollgrou0_.name must be GROUPed or used in an aggregate function

I can't group by pg.name, though, because that field is not necessarily unique.

Carl-Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2003 11:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I tried using "SELECT DISTINCT pg.name, pd.id ...", but that gave me a QueryException about expecting an aggregate function


Huh!!?? Are you sure? That should work perfectly.

The corrrect query is:

Code:
SELECT pg.name, pg.id
FROM PollGroup pg
LEFT JOIN pg.polls p
WHERE
pg.owner = :usr
OR :usr IN elements(pg.pollCreators)
OR p.owner = :usr
OR :usr IN elements(p.editors)
ORDER BY pg.name


or, probably even better:

Code:
SELECT pg.name, pg.id
FROM PollGroup pg
LEFT JOIN pg.polls p
LEFT JOIN p.editors ed
LEFT JOIN pg.pollCreators c
WHERE pg.owner = :usr
OR ed = :usr
OR p.owner = :usr
OR c = :usr
ORDER BY pg.name


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 4:46 am 
Newbie

Joined: Tue Sep 02, 2003 8:19 pm
Posts: 19
gavin wrote:
Quote:
I tried using "SELECT DISTINCT pg.name, pd.id ...", but that gave me a QueryException about expecting an aggregate function


Huh!!?? Are you sure? That should work perfectly.



Errrmm. Rewriting the whole damn thing complete with the DISTINCT seems to have fixed it. The SELECT DISTINCT does indeed work. How embarrassing. I remember now: I used DISTINCT (pg.name, pg.id), which doesn't seem to work.

Quote:

The corrrect query is:



Ah, these look interesting. I'll have to read some more about joins, I don't seem to always understand them correctly. (Any good suggestions about further reading?)

I tried the second query - it works, but it also gives me duplicate rows. Shouldn't the left join prevent that? Or am I only showing my ignorance of joins here?

Thanks a lot!
Carl-Eric


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 8:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Any good suggestions about further reading?


Just pick up a book that covers ANSI standard SQL (O'Reilly has a good one)

Quote:
I tried the second query - it works, but it also gives me duplicate rows.


Oh, you may still need the DISTINCT keyword.


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.