aspose file tools
The moose likes JDBC and the fly likes Count on a subquery Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Count on a subquery" Watch "Count on a subquery" New topic
Author

Count on a subquery

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 176
Hi all,
Is it possible in SQL to do a count on a subquery? For example..

select parent_name , count (select child_id from child a where a.id = p.id) from parent p group by parent_name

I know that SQL is wrong. It is meant only to be psuedo-code, but I hope you get the idea.

Thanks for any help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Not sure, but I think yes.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

Can you try

select p.parent_name , (select count (a.child_id) from child a where a.id = p.id) from parent p




attention: untested.


OCUP UML fundamental
ITIL foundation
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26199
    
  66

This works in postgresql. Not sure if it would work in all databases, but it is fast to try in yours.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Count on a subquery
 
Similar Threads
HQL query hibernate
hibernate how to make a count and group by
Error While Creating Materialized View
how to use variable with oracle like
subquery sql to hql