p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Server 2000 (http://p2p.wrox.com/forumdisplay.php?f=20)
-   -   Select Sql Query (http://p2p.wrox.com/showthread.php?t=58001)

kk_kumar99 May 17th, 2007 07:06 AM

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

happygv May 17th, 2007 08:40 AM

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

_________________________
- Vijay G
Strive for Perfection

sshelper May 17th, 2007 09:23 AM

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

robprell May 17th, 2007 05:21 PM

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.


kk_kumar99 May 18th, 2007 03:55 AM

This query is working fine.Many Thanks.

Regards
Kapil




sshelper May 18th, 2007 03:36 PM

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


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

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