Click here to monitor SSC
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
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
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 Moden's 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 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)

Group: General Forum Members
Points: 526 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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45137 Visits: 39923
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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: 1454 Visits: 136
What are you expecting?
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 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