Wrox Programmer Forums
| 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
  #1 (permalink)  
Old May 17th, 2007, 07:06 AM
Registered User
 
Join Date: Oct 2006
Location: Faridabad, Haryana, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Sql Query

Hi,

I have 2 tables. One is containing Bank branch name and 2 have cutomer Id and branch name from table 1.

I want to write a query which gives me all the cutomers having account in all branches.

Please help me in this.

Regards
Kapil Kumar
  #2 (permalink)  
Old May 17th, 2007, 08:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

hmmm! I am thinking... if a customer can have account in all the branches. Is that possible logically?

_________________________
- Vijay G
Strive for Perfection
  #3 (permalink)  
Old May 17th, 2007, 09:23 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can try this query:

SELECT CustomerID
FROM Customers A INNER JOIN Branches B ON A.BranchName = B.BranchName
GROUP BY CustomerID
HAVING COUNT(*) = (SELECT COUNT(*) FROM Branches)

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
  #4 (permalink)  
Old May 17th, 2007, 05:21 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The way the question is stated I am not quite sure what you are looking for. But if sshelper's answer is not what your asking, you may be looking for a "full outer join" read about it in the help files.

  #5 (permalink)  
Old May 18th, 2007, 03:55 AM
Registered User
 
Join Date: Oct 2006
Location: Faridabad, Haryana, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This query is working fine.Many Thanks.

Regards
Kapil



  #6 (permalink)  
Old May 18th, 2007, 03:36 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just a warning to the query I provided. If the customer has accounts in all branches and in some branches he has more than 1, that customer will not be returned by the query because his total accounts is more than the number of branches.

Another possible scenario where the query will not return proper results is when the customer has multiple accounts in one branch but none in the other branches and still his total number of accounts matches the total number of branches.

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Query gregalb SQL Server 2000 2 November 15th, 2007 10:56 PM
SELECT from SELECT query? seananderson Access 1 October 12th, 2007 12:40 AM
Help for Select SQL Query with date filter pushpa SQL Server DTS 0 February 6th, 2007 10:06 AM
Select Query Help dbartelt Access 1 June 13th, 2005 08:06 AM
select query collie SQL Server 2000 2 January 17th, 2005 03:13 AM





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