Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 17th, 2007, 11:57 AM
Authorized User
 
Join Date: Sep 2007
Location: Delhi, Delhi, India.
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Query Problem

I have two tables one is A cols ID,Name and other table B cols ID, A_ID,B_NAME

I need result Name from A and its correponding count from B ( in B table I store many entries with relation A)

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old December 17th, 2007, 01:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

A correlated subquery is probably the simplest:

SELECT A.NAME, (SELECT Count(*) FROM B WHERE A.ID = B.A_ID) as BCount
  FROM A;

This could also be done by JOINing the tables and GROUPing by ID (you can't group just by name because of potential duplicate names for different IDs) to get the count:

SELECT A.ID, A.NAME, Count(*) as BCount
  FROM A
 INNER JOIN B ON A.ID = B.A_ID
 GROUP BY A.ID, A.NAME;

It's not clear to me which is more efficient.

Jeff Mason
je.mason@comcast.net
Reply With Quote
  #3 (permalink)  
Old December 18th, 2007, 04:59 AM
Authorized User
 
Join Date: Sep 2007
Location: Delhi, Delhi, India.
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff its working
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT from SELECT query? seananderson Access 1 October 12th, 2007 12:40 AM
Need help on select query arul1984 SQL Server 2000 2 July 4th, 2007 01:49 AM
Select Query Help dbartelt Access 1 June 13th, 2005 08:06 AM
Select Query String Problem phungleon SQL Server 2000 8 June 9th, 2004 05:02 PM
Select query problem jackps SQL Server 2000 1 January 19th, 2004 07:30 AM



All times are GMT -4. The time now is 05:05 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.