-->
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: Criteria Group By Problem
PostPosted: Thu Apr 21, 2011 3:50 pm 
Newbie

Joined: Mon Dec 14, 2009 5:38 am
Posts: 13
Hello,
I'm so confused with Group By using Criteria by reading tips/articles on the internet,
so I hope someone can help me here...

I have a table:
City, Name, Donation
------------------------
Jakarta, Helen, $20
Jakarta, Boy, $5
Jakarta, Helen, $10
Bandung, Rudy, $3
Bandung, Linda, $4

I can group by the City and sum the Donation so that the table like this:
City, Donation
----------------
Jakarta, $35
Bandung, $7

but I don't know how to group by the Name and sum Donation, so that the table will be like this:
City, Name, Donation
------------------------
Jakarta, Helen, $30
Jakarta, Boy, $5
Bandung, Rudy, $3
Bandung, Linda, $4

(I can group by the Name, but the City doesn't come up)
please help,
Faizal


Top
 Profile  
 
 Post subject: Re: Criteria Group By Problem
PostPosted: Fri Apr 22, 2011 5:14 am 
Newbie

Joined: Thu Mar 31, 2011 4:56 am
Posts: 3
Hi !

You have to group by the City and the Name in order to have the same result.

something like :

select t.city, t.name, sum(t.donation)
from yourtable as t
group by t.city, t.name


Top
 Profile  
 
 Post subject: Re: Criteria Group By Problem
PostPosted: Fri Apr 22, 2011 10:21 am 
Newbie

Joined: Fri Apr 22, 2011 10:16 am
Posts: 6
Hi try this criteria

Criteria crit = session.createCriteria(ForumBeanValue.class)
.setProjection(
Projections.projectionList()
.add(Projections.groupProperty("city"))
.add(Projections.groupProperty("name"))
.add(Projections.sum("donate"))

);


Top
 Profile  
 
 Post subject: Re: Criteria Group By Problem
PostPosted: Tue Apr 26, 2011 10:59 pm 
Newbie

Joined: Mon Dec 14, 2009 5:38 am
Posts: 13
Hello all, thanks for your replies,
what if my table is like this:

City, Name, Donation
------------------------
Jakarta, Helen, $20
Jakarta, Boy, $5
Jakarta, Helen, $10
Bandung, Rudy, $3
Bandung, Linda, $4
Bandung, Helen, $6

and I want to group by Name and sum Donation, so I can have the result like this:

City, Name, Donation
------------------------
Jakarta, Helen, $30
Jakarta, Boy, $5
Bandung, Rudy, $3
Bandung, Linda, $4
Bandung, Helen, $6

as the result I want above, the City is not grouped for Name Helen

can I do this using Criteria?


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.