SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Join the two table


How to Join the two table

Author
Message
ramanathan.raman88
ramanathan.raman88
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 42
Table 1

A B C D F

A1 B1 NULL NULL NULL
A2 B2 NULL NULL NULL
A3 B3 NULL NULL NULL
A4 B4 NULL NULL NULL
A5 B5 NULL NULL NULL

Table 2

Loc Name

A1 White
A2 Black
A3 Red
A4 Orange
A5 Blue
B1 Green
B2 Yellow
B3 Gold
B4 Rose
B5 Silver
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26319 Visits: 17553
Use an OR condition in your join statement?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
josh.granville
josh.granville
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 56
ramanathan.raman88 (2/2/2013)
Table 1

A B C D F

A1 B1 NULL NULL NULL
A2 B2 NULL NULL NULL
A3 B3 NULL NULL NULL
A4 B4 NULL NULL NULL
A5 B5 NULL NULL NULL

Table 2

Loc Name

A1 White
A2 Black
A3 Red
A4 Orange
A5 Blue
B1 Green
B2 Yellow
B3 Gold
B4 Rose
B5 Silver


So your just asking how to join the two tables..?
Snigdha Vartak
Snigdha Vartak
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 247
Hi,

What are trying to do? Use Left / Right / Inner joins as required.

SELECT A,B,C,D,E,F,Name FROM TABLE1 LEFT JOIN TABLE2 ON A=Loc

SELECT A,B,C,D,E,F,Name FROM TABLE1 RIGHT JOIN TABLE2 ON Loc=A

SELECT A,B,C,D,E,F,Name FROM TABLE1 INNER JOIN TABLE2 ON A=Loc

Refer: http://www.w3schools.com/sql/sql_join.asp
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87298 Visits: 41113
ramanathan.raman88 (2/2/2013)
Table 1

A B C D F

A1 B1 NULL NULL NULL
A2 B2 NULL NULL NULL
A3 B3 NULL NULL NULL
A4 B4 NULL NULL NULL
A5 B5 NULL NULL NULL

Table 2

Loc Name

A1 White
A2 Black
A3 Red
A4 Orange
A5 Blue
B1 Green
B2 Yellow
B3 Gold
B4 Rose
B5 Silver



The absolute best way would be to normalize Table 1. ;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 136
What are you expecting?
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7395 Visits: 6431
Looks like homework, but if what you're after is the Names associated with columns A and B, perhaps a correlated sub-query would be best:


CREATE TABLE #Table1
(A CHAR(2), B CHAR(2), C VARCHAR(20), D VARCHAR(20), F VARCHAR(20));

INSERT INTO #Table1
SELECT 'A1','B1',NULL, NULL, NULL
UNION ALL SELECT 'A2','B2',NULL, NULL, NULL
UNION ALL SELECT 'A3','B3',NULL, NULL, NULL
UNION ALL SELECT 'A4','B4',NULL, NULL, NULL
UNION ALL SELECT 'A5','B5',NULL, NULL, NULL;

CREATE TABLE #Table2
(Loc CHAR(2), Name VARCHAR(20));

INSERT INTO #Table2
SELECT 'A1','White'
UNION ALL SELECT 'A2','Black'
UNION ALL SELECT 'A3','Red'
UNION ALL SELECT 'A4','Orange'
UNION ALL SELECT 'A5','Blue'
UNION ALL SELECT 'B1','Green'
UNION ALL SELECT 'B2','Yellow'
UNION ALL SELECT 'B3','Gold'
UNION ALL SELECT 'B4','Rose'
UNION ALL SELECT 'B5','Silver';

SELECT A, B
,NameOfA=(SELECT Name FROM #Table2 b WHERE a.A = b.Loc)
,NameOfB=(SELECT Name FROM #Table2 b WHERE a.B = b.Loc)
FROM #Table1 a;

GO
DROP TABLE #Table1, #Table2;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search