-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to get row count of a query with group by?
PostPosted: Wed Jun 07, 2006 8:32 am 
I'm writing an application where users can formulate their queries on datasets (=entities) graphically, allowing them to hide columns, specify constraints and operators on them. The results are then shown in pages.

In order to be able to display something like 'Page X of Y' I need the total count of rows the query would return if it was not paginated.

This is easy for a plain query with arbitrary constraints, but not possible when one of the result set columns is a count, grouped by another column, like in this example the count of employees per department:

Code:
count      |    department
-----------+----------------
13         |          Sales
45         |    Development
2         |      Logistics


Ideally I would like to be able to execute a count on the (then) nested HQL or, at least, be able to obtain the generated SQL and do the count myself.

Any insights are appreciated
G.


Top
  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 10:11 am 
Newbie

Joined: Sun May 28, 2006 9:43 am
Posts: 6
This is a bit of a dirty solution, but you can do something along the lines of

Code:
select (select count(*) from A), name, count(*) from A group by name


Hope this helps,
Yair


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 10:32 am 
Thank you for your reply.

I think that is not quite what I want.

Code:
select name, count(*) from A group by name


would, for instance, return 100 rows, each looking like

Code:
(name, count-per-name).


The added select count(*) from A may return 6000, but that only leads to that the rows from my first query look like (6000, name, count-per-name).

And there is of course another problem, the count of A is not the count of rows the group by returns (in my example that would be 100, not 6000).
g.


Top
  
 
 Post subject: Re: How to get row count of a query with group by?
PostPosted: Wed Jun 07, 2006 12:12 pm 
Newbie

Joined: Wed Jun 07, 2006 10:07 am
Posts: 5
georgeuoa wrote:
In order to be able to display something like 'Page X of Y' I need the total count of rows the query would return if it was not paginated.

This is easy for a plain query with arbitrary constraints, but not possible when one of the result set columns is a count, grouped by another column, like in this example the count of employees per department:

Code:
count      |    department
-----------+----------------
13         |          Sales
45         |    Development
2         |      Logistics


Ideally I would like to be able to execute a count on the (then) nested HQL or, at least, be able to obtain the generated SQL and do the count myself.


I don't understand, you want the count of rows for that example you posted? That is, you want to return "3" ?

If that's the case you could do:
int rows=session.createQuery("select whatever").list().size();

Messy and will take a lot of memory but does the job (If I understood your question correctly, of course).


Top
 Profile  
 
 Post subject: Re: How to get row count of a query with group by?
PostPosted: Thu Jun 08, 2006 4:04 am 
hjf wrote:
I don't understand, you want the count of rows for that example you posted? That is, you want to return "3" ?

If that's the case you could do:
int rows=session.createQuery("select whatever").list().size();

Messy and will take a lot of memory but does the job (If I understood your question correctly, of course).


Thank you hjf for your reply!
Yes, that exactly is what I want to do. Your solution has been suggested elsewhere in the forum too, and unfortunately your assessment of it is also correct. One of the reasons for pagination is that there are many results to display, in my case prohibitively many, and query.list().size() would, as far as I grasp the mechanics of it, at least read every record (I don't think it actually would store it in memory) and thus count them by enumeration - that is too slow and causes excessive network load.

G.


Top
  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 2:26 pm 
Newbie

Joined: Wed Jun 07, 2006 10:07 am
Posts: 5
OK here's your answer then. This applies to MYSQL 4 and newer I think:

suppose I have a table "test" with an ID field and a val1 field. with 6 "a" values, 4 "b" values and 4 "c values. I do:

select count(val1), val1 from test group by val1;

and i get:

6 A
4 B
4 C


I want the count of the number of rows actually, so I do:

select count(count1) from (SELECT val1, count(val1) as count1 from test group by val1) as count1;

count(count1):
3

Here's the link: http://dev.mysql.com/doc/refman/4.1/en/ ... views.html
Hope that helps!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 4:42 am 
Hello again hjf!

SQL would work, but I am writing HQL queries since I need some DB independence. The possibility of generating a SQL also occured to me - that's why I posted in my first entry the question of how to get the generated SQL from the HQL.

If that fails too then I must of course go back to SQL...
Thanx again
G.


Top
  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 7:43 am 
Dear all

just writing to let you know that I found a workaround. It doesn't make me happy since it entails the application of an SQLQuery, but that seems my only option now.

In detail, the HQL which I want the row count from is almost an SQL, so only the FROM argument has to change from FROM Entity to FROM Table, the rest I can wrap around with a SELECT COUNT(*) FROM (HQL)

Thank you for your input
G.


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