;with names as (
select distinct t1.name,rtrim(substring(isnull((select ','+convert(varchar,t2.skill) from #names t2 where t1.name = t2.name for xml path('')),' '),2,2000)) skill
from #names t1)
,jobs as (
select distinct t1.job,rtrim(substring(isnull((select ','+convert(varchar,t2.skill) from #jobs t2 where t1.job = t2.job for xml path('')),' '),2,2000)) skill
from #jobs t1)
select n.name,j.job
from names n
join jobs j on n.skill like '%'+j.skill+'%'
IF OBJECT_ID('tempdb..#names') IS NOT NULL DROP TABLE #names
CREATE TABLE #names(
name varchar(30),
skill int)
IF OBJECT_ID('tempdb..#jobs') IS NOT NULL DROP TABLE #jobs
CREATE TABLE #jobs(
job varchar(30),
skill int)
INSERT INTO #names
SELECT 'name1', 1 UNION ALL
SELECT 'name2', 1 UNION ALL
SELECT 'name2', 2 UNION ALL
SELECT 'name3', 2
INSERT INTO #jobs
SELECT 'job1', 1 UNION ALL
SELECT 'job2', 1 UNION ALL
SELECT 'job2', 2 UNION ALL
SELECT 'job3', 2
;with names as (
select distinct t1.name,rtrim(substring(isnull((select ','+convert(varchar,t2.skill) from #names t2 where t1.name = t2.name for xml path('')),' '),2,2000)) skill
from #names t1)
,jobs as (
select distinct t1.job,rtrim(substring(isnull((select ','+convert(varchar,t2.skill) from #jobs t2 where t1.job = t2.job for xml path('')),' '),2,2000)) skill
from #jobs t1)
select n.name,j.job
from names n
join jobs j on n.skill like '%'+j.skill+'%'
DROP TABLE #names
DROP TABLE #jobs
/*
name job
name1 job1
name2 job1
name2 job2
name2 job3
name3 job3
*/
Open in new window