473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help with joining 2 tables

Hi, I'm a beginner in sql so hopefully the masters in here can help me out with a simple problem...

I have 2 tables:

A) BANK_TABLE

BANK_ID BANK_NAME
B001 AAA
B002 BBB
B003 CCC
B004 DDD
...

B) CUSTOMERBANK_TABLE

CUST_ID BANK_ID
C001 B001
C001 B003
C005 B001
C005 B002
C010 B003
C015 B002
C015 B004
...

BANK_TABLE is the master bank table. CUSTOMERBANK_TABLE is linked to BANK_TABLE by BANK_ID (obviously) and stores all the customers' selected banks.

What I want to do is, given a CUST_ID (eg C015) I want to return all the banks from BANK_TABLE, and if a given bank is selected by C015, it will return the CUST_ID. This is the structure that I want the result:

CUST_ID BANK_ID BANK_NAME
<null> B001 AAA
C015 B002 BBB
<null> B003 CCC
C015 B004 DDD
<null> B005 EEE



So the query will return all the banks, and if a bank is selected by a customer who's not C015, it'll return a null/blank CUST_ID, and if the bank is selected by C015, it'll return this CUST_ID.

I hope this is clear enough, will appreciate any feedback :)
Mar 17 '08 #1
5 1111
debasisdas
8,127 Expert 4TB
The join in this case is so simple

Kindly post your query that you are working on.
Mar 17 '08 #2
The join in this case is so simple

Kindly post your query that you are working on.
Exactly what I'm looking to hear :)

Here is my query:

select c.cust_id, b.bank_id, b.bank_name
from BANK b
left outer join CUSTBANK c on c.bank_id = b.bank_id


Im getting this result based on the examples above:

CUST_ID BANK_ID BANK_NAME
C001 B001 AAA
C005 B001 AAA
C005 B002 BBB
C015 B002 BBB
C001 B003 CCC
C010 B003 CCC
C015 B004 DDD

So assuming im sending a parameter custid=C015, i want the result to return all the banks (unique only), and if a bank is selected by C015, this cust_id will be returned as part of the result as explained in my first post.

Thanks!
Mar 17 '08 #3
anyone? i believe this is a simple problem but for the life of me i couldn't get it... :(
Mar 18 '08 #4
Delerna
1,134 Expert 1GB
How about this in a UDF
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION fnListCustomerBanks( @CustNo varchar(10) )
  2. RETURNS @tbl table (CustID varchar(10),
  3.                     BankID varchar(10),
  4.                     BankName varchar(10))
  5. AS
  6. BEGIN
  7.  
  8.     INSERT INTO @tbl
  9.     SELECT case When b.CustID=@CustNo then Cust_ID else null end  as Cust_ID,
  10.            Bank_ID,BankName 
  11.     FROM Bank_Table a
  12.     LEFT JOIN CustomerBank_Table b on a.Bank_ID=b.BankID
  13.  
  14.     RETURN
  15. END
  16.  

then you use the function like a parameterised query

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM dbo.fnListCustomerBanks('C015')
  3.  
Mar 18 '08 #5
Delerna
1,134 Expert 1GB
actually that will return the same bank multiple times
so this version of the UDF
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION fnListCustomerBanks( @CustNo varchar(10) )
  2. RETURNS @tbl table (CustID varchar(10),
  3.                     BankID varchar(10),
  4.                     BankName varchar(10))
  5. AS
  6. BEGIN
  7.  
  8.     INSERT INTO @tbl
  9.     SELECT max(case When b.CustID=@CustNo then Cust_ID else null end)  as Cust_ID,
  10.            Bank_ID,BankName 
  11.     FROM Bank_Table a
  12.     LEFT JOIN CustomerBank_Table b on a.Bank_ID=b.BankID
  13.     GROUP BY Bank_ID,BankName 
  14.     RETURN
  15. END
  16.  
should fix that.
Mar 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one...
4
by: sdowney717 | last post by:
Select LOCGeneralHave.LSCH, LOCSubClassHave.LSCH from LOCGeneralHave , LOCSubClassHave Where (LOCGeneralHave.LCNT <> '0' and LOCSubClassHave.LCNT = '0') This query seems to be ignoring the...
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
3
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: ...
4
by: lorirobn | last post by:
Hello, I'd be curious to hear other thoughts on my database and table design. My database is for 'space use' in a lodging facility - will hold all spaces, like rooms (lodging rooms, dining...
12
by: eyoung | last post by:
Can someone tell me what I've done wrong here...this kinda works...but no cigar! I want to loop threw each "Service Number" and get all entries before going on to the next. What looks something...
3
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both...
3
by: isaac2004 | last post by:
hello i am making a spoof online book store site for a class and I was wondering how i could fix a problem i am having. I have two tables, one the cart and the other a table with book descriptions....
1
by: mkhalid | last post by:
Hi... I am doing an assignment of MS access. I have made 3 tables (One joining table for M:M relationship). When I was trying to make a form (a form with subform) to update the fields, an error...
4
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.