Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 JOIN on Alternative Column

Author  Topic 

Kristen
Test

22859 Posts

Posted - 2013-10-18 : 10:29:23
I am importing some data from another system. The names they have for countries are different in some instances, so in our Country Lookup Table I have column for "What the other guy thinks it is".

However ... for a given country the other system sometimes has matching spelling to us, sometime an alternative, so I can't just do "Match theirs, if there is one, otherwise match ours".

Also, I must avoid a situation where a record gets matched twice (i.e. Description-X appears in one Country lookup Record in Our column, and on a different record in Their column

The code I have come up with is a little clunky, maybe there is a better (as in "more efficient for the server") way? Perhaps using a CTE?

DECLARE @Table1 TABLE
(
T1_ID int,
T1_MatchColumn1 varchar(30),
T1_MatchColumn2 varchar(30),
PRIMARY KEY
(
T1_ID
)
)

DECLARE @Table2 TABLE
(
T2_ID int,
T2_ShouldMatch int,
T2_ValueColumn1 varchar(30),
PRIMARY KEY
(
T2_ID
)
)

INSERT INTO @Table1
SELECT 1, 'Value 1', 'OtherValue 1'
UNION ALL SELECT 2, 'Value 2', 'OtherValue 2'
UNION ALL SELECT 3, 'Value X1', 'Value X2'
UNION ALL SELECT 4, 'Value X2', 'Value X1'

INSERT INTO @Table2
SELECT 1,1, 'Value 1'
UNION ALL SELECT 2,1, 'OtherValue 1'
UNION ALL SELECT 3,2, 'Value 2'
UNION ALL SELECT 4,2, 'OtherValue 2'
UNION ALL SELECT 5,3, 'Value X1'
UNION ALL SELECT 6,4, 'Value X2'

PRINT '[1] Flawed - matches 5 & 6 twice'
SELECT CASE WHEN T2_ShouldMatch = T1_ID THEN '' ELSE 'ERR' END,
*
FROM @Table2 AS T2
JOIN @Table1 AS T1
ON T1_MatchColumn1 = T2_ValueColumn1
OR T1_MatchColumn2 = T2_ValueColumn1
--
ORDER BY T2_ID, T1_ID

PRINT '[2] Flawed - fails to match 2 & 4'
SELECT CASE WHEN T2_ShouldMatch = T1_ID THEN '' ELSE 'ERR' END,
*
FROM @Table2 AS T2
JOIN @Table1 AS T1
ON COALESCE(T1_MatchColumn1, T1_MatchColumn2) = T2_ValueColumn1
--
ORDER BY T2_ID, T1_ID


PRINT '[3] Works OK but too clunky?'
SELECT CASE WHEN T2_ShouldMatch = T1_ID THEN '' ELSE 'ERR' END,
*
FROM @Table2 AS T2
JOIN
(
SELECT T1_ID,
T1_MatchColumn
FROM
(
SELECT [T_RowNumber] = ROW_NUMBER()
OVER
(
PARTITION BY T1_MatchColumn
ORDER BY T1_MatchColumn, T1_Source
),
X.*
FROM
(
SELECT T1_ID, [T1_MatchColumn] = T1_MatchColumn1, [T1_Source] = 1
FROM @Table1
UNION ALL
SELECT T1_ID, T1_MatchColumn2, 2
FROM @Table1
) AS X
) AS T
WHERE T_RowNumber = 1

) AS T1
ON T1_MatchColumn = T2_ValueColumn1
--
ORDER BY T2_ID, T1_ID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-18 : 14:02:09
[code]

DECLARE @Table1 TABLE
(
T1_ID int,
T1_MatchColumn1 varchar(30),
T1_MatchColumn2 varchar(30),
PRIMARY KEY
(
T1_ID
)
)

DECLARE @Table2 TABLE
(
T2_ID int,
T2_ShouldMatch int,
T2_ValueColumn1 varchar(30),
PRIMARY KEY
(
T2_ID
)
)

INSERT INTO @Table1
SELECT 1, 'Value 1', 'OtherValue 1'
UNION ALL SELECT 2, 'Value 2', 'OtherValue 2'
UNION ALL SELECT 3, 'Value X1', 'Value X2'
UNION ALL SELECT 4, 'Value X2', 'Value X1'

INSERT INTO @Table2
SELECT 1,1, 'Value 1'
UNION ALL SELECT 2,1, 'OtherValue 1'
UNION ALL SELECT 3,2, 'Value 2'
UNION ALL SELECT 4,2, 'OtherValue 2'
UNION ALL SELECT 5,3, 'Value X1'
UNION ALL SELECT 6,4, 'Value X2'

SELECT t2.*,COALESCE(t11.T1_ID,t12.T1_ID) AS T1_ID,
COALESCE(t11.T1_MatchColumn1,t12.T1_MatchColumn2) AS MatchColumn
FROM @Table2 t2
LEFT JOIN @Table1 t11
ON t11.T1_MatchColumn1 = t2.T2_ValueColumn1
LEFT JOIN @Table1 t12
ON t12.T1_MatchColumn2 = t2.T2_ValueColumn1
AND t11.T1_MatchColumn1 IS NULL


output
------------------------------------------------------------
T2_ID T2_ShouldMatch T2_ValueColumn1 T1_ID MatchColumn
------------------------------------------------------------
1 1 Value 1 1 Value 1
2 1 OtherValue 1 1 OtherValue 1
3 2 Value 2 2 Value 2
4 2 OtherValue 2 2 OtherValue 2
5 3 Value X1 3 Value X1
6 4 Value X2 4 Value X2

[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2013-10-19 : 04:56:23
Thanks Visakh, I prefer that as I can easily index both columns, and the pair of JOINs will thus be efficient.

I had had a similar thought already and hit a snag (the real world data has some wider implications than my test sample), but I'll give it a crack and come back if I hit a brick wall.

Has Graz fixed the 99,999 post bug for you yet?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-20 : 03:52:47
quote:
Originally posted by Kristen

Thanks Visakh, I prefer that as I can easily index both columns, and the pair of JOINs will thus be efficient.

I had had a similar thought already and hit a snag (the real world data has some wider implications than my test sample), but I'll give it a crack and come back if I hit a brick wall.

Has Graz fixed the 99,999 post bug for you yet?



Not yet
Hopefully He'll get it fixed before I reach there

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -