Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Count on a subquery

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49445
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but I think yes.
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34680
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works in postgresql. Not sure if it would work in all databases, but it is fast to try in yours.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic