-->
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.  [ 5 posts ] 
Author Message
 Post subject: Ordering a COUNT() column
PostPosted: Thu Apr 14, 2005 2:22 pm 
Newbie

Joined: Fri Aug 06, 2004 8:10 am
Posts: 9
Location: Brazil
Hibernate version:2.1.6

I have a result query with a aggregate function COUNT():

"select u.iduser, count(u.iduser) from users u"

I need to order this query by the COUNT() column. Is that possible? How can i apply the ORDER BY clause on this column?

TIA

Marcio.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 8:25 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
try it - it is possible in hibernate 3

regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 3:14 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Two things:
You may want to group by the aggregate column:
Code:
"select u.iduser, count(u.iduser) from users u group by u.iduser"

and you may want to alias your column selection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 2:03 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
I can't seem to get this to work. ClubMember has a many-to-one link back to Club.

Code:
select members.club, count(members.id) memTotal from ClubMember members group by members.club order by memTotal


Produces the following error using Eclipse Hibernate Console with 3.0.2:

Code:
org.hibernate.QueryException: , expected in SELECT [select members.club, count(members.id) memTotal from p2p.common.clubs.ClubMember members group by members.club order by memTotal ]
   at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:175)
   at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86)
   at org.hibernate.hql.classic.ClauseParser.end(ClauseParser.java:113)
   at org.hibernate.hql.classic.PreprocessingParser.end(PreprocessingParser.java:122)
   at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:30)
   at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:172)
   at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:148)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:410)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:987)
   at org.hibernate.impl.SessionImpl.find(SessionImpl.java:964)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at org.hibernate.console.HQLQueryPage.getList(HQLQueryPage.java:30)
   at org.hibernate.eclipse.console.views.QueryPageViewer$ContentProviderImpl.getElements(QueryPageViewer.java:74)
   at org.eclipse.jface.viewers.StructuredViewer.getRawChildren(StructuredViewer.java:765)
   at org.eclipse.jface.viewers.TableViewer.getRawChildren(TableViewer.java:1046)
   at org.eclipse.jface.viewers.StructuredViewer.getFilteredChildren(StructuredViewer.java:707)
   at org.eclipse.jface.viewers.StructuredViewer.getSortedChildren(StructuredViewer.java:822)
   at org.eclipse.jface.viewers.TableViewer.internalRefreshAll(TableViewer.java:762)
   at org.eclipse.jface.viewers.TableViewer.internalRefresh(TableViewer.java:712)
   at org.eclipse.jface.viewers.TableViewer.internalRefresh(TableViewer.java:701)
   at org.eclipse.jface.viewers.StructuredViewer$7.run(StructuredViewer.java:1171)
   at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1108)
   at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1169)
   at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1128)
   at org.eclipse.jface.viewers.TableViewer.inputChanged(TableViewer.java:662)
   at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:248)
   at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1324)
   at org.hibernate.eclipse.console.views.QueryPageViewer.createTable(QueryPageViewer.java:168)
   at org.hibernate.eclipse.console.views.QueryPageViewer.createControl(QueryPageViewer.java:147)
   at org.hibernate.eclipse.console.views.QueryPageViewer.<init>(QueryPageViewer.java:105)
   at org.hibernate.eclipse.console.views.QueryPageTabView.rebuild(QueryPageTabView.java:78)
   at org.hibernate.eclipse.console.views.QueryPageTabView$1.contentsChanged(QueryPageTabView.java:47)
   at org.hibernate.eclipse.console.views.QueryPageTabView$1.intervalAdded(QueryPageTabView.java:52)
   at javax.swing.AbstractListModel.fireIntervalAdded(AbstractListModel.java:130)
   at org.hibernate.console.QueryPageModel.add(QueryPageModel.java:50)
   at org.hibernate.console.KnownConfigurations$3.queryPageCreated(KnownConfigurations.java:167)
   at org.hibernate.console.ConsoleConfiguration.fireQueryPageCreated(ConsoleConfiguration.java:265)
   at org.hibernate.console.ConsoleConfiguration.executeHQLQuery(ConsoleConfiguration.java:246)
   at org.hibernate.eclipse.console.actions.ExecuteHQLAction.execute(ExecuteHQLAction.java:99)
   at org.hibernate.eclipse.console.actions.ExecuteHQLAction.run(ExecuteHQLAction.java:94)
   at org.eclipse.jface.action.Action.runWithEvent(Action.java:996)
   at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:538)
   at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:488)
   at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:441)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:842)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2894)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2527)
   at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1570)
   at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1534)
   at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:306)
   at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:143)
   at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:103)
   at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:228)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:156)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.eclipse.core.launcher.Main.invokeFramework(Main.java:315)
   at org.eclipse.core.launcher.Main.basicRun(Main.java:268)
   at org.eclipse.core.launcher.Main.run(Main.java:942)
   at org.eclipse.core.launcher.Main.main(Main.java:926)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 3:30 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
In the end, this is the SQL I'm looking for, which functions correctly:

Code:
select member.clubId, count(member.id) as counter from ClubMember member group by member.clubId order by counter desc limit 5


The following HQL works correctly, but does not have an order by clause:

Code:
select members.club, count(members) from ClubMember members group by members.club limit 5


Most queries I attempt give the ", expected" error. The following queries give a different result.

Code:
select members.club, count(members) from ClubMember members group by members.club order by count(members) desc limit 5


Error is:

Code:
java.sql.SQLException: Invalid use of group function


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.