Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Count on a subquery  RSS feed

 
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
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but I think yes.
 
Jan Cumps
Bartender
Posts: 2628
17
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
Sheriff
Posts: 36417
454
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
Boost this thread!