February 2, 2013 at 2:45 am
Table 1
ABCDF
A1B1NULLNULLNULL
A2B2NULLNULLNULL
A3B3NULLNULLNULL
A4B4NULLNULLNULL
A5B5NULLNULLNULL
Table 2
LocName
A1White
A2Black
A3Red
A4Orange
A5Blue
B1Green
B2Yellow
B3Gold
B4Rose
B5Silver
February 4, 2013 at 8:03 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 26, 2013 at 9:32 am
ramanathan.raman88 (2/2/2013)
Table 1ABCDF
A1B1NULLNULLNULL
A2B2NULLNULLNULL
A3B3NULLNULLNULL
A4B4NULLNULLNULL
A5B5NULLNULLNULL
Table 2
LocName
A1White
A2Black
A3Red
A4Orange
A5Blue
B1Green
B2Yellow
B3Gold
B4Rose
B5Silver
So your just asking how to join the two tables..?
August 9, 2013 at 3:14 pm
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
August 9, 2013 at 8:38 pm
ramanathan.raman88 (2/2/2013)
Table 1ABCDF
A1B1NULLNULLNULL
A2B2NULLNULLNULL
A3B3NULLNULLNULL
A4B4NULLNULLNULL
A5B5NULLNULLNULL
Table 2
LocName
A1White
A2Black
A3Red
A4Orange
A5Blue
B1Green
B2Yellow
B3Gold
B4Rose
B5Silver
The absolute best way would be to normalize Table 1. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2013 at 1:20 am
What are you expecting?
October 10, 2013 at 10:07 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply