Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to Join the two table Expand / Collapse
Author
Message
Posted Saturday, February 2, 2013 2:45 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, March 31, 2014 3:07 AM
Points: 99, Visits: 40
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

Post #1414927
Posted Monday, February 4, 2013 8:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 9:10 AM
Points: 13,230, Visits: 12,709
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)
Post #1415323
Posted Wednesday, June 26, 2013 9:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, September 28, 2014 8:07 PM
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..?
Post #1467743
Posted Friday, August 9, 2013 3:14 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:58 PM
Points: 522, Visits: 241
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
Post #1482953
Posted Friday, August 9, 2013 8:38 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:55 PM
Points: 35,618, Visits: 32,214
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1482996
Posted Tuesday, October 8, 2013 1:20 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, November 12, 2014 9:37 PM
Points: 1,454, Visits: 136
What are you expecting?
Post #1502477
Posted Thursday, October 10, 2013 10:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
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!
Post #1503860
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse