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 | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
  #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.

Reply With Quote
  #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



Reply With Quote
  #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
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 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



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


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