SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 JOIN on Alternative Column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Kristen
Test

United Kingdom
22403 Posts

Posted - 10/18/2013 :  10:29:23  Show Profile  Reply with Quote
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


Edited by - Kristen on 10/18/2013 10:29:57

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/18/2013 :  14:02:09  Show Profile  Reply with Quote


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



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 10/18/2013 14:03:42
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 10/19/2013 :  04:56:23  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/20/2013 :  03:52:47  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000