Может быть, что-то вроде этого:
Сначала данные испытаний:
CREATE TABLE #tblUser
(
userid INT,
userName VARCHAR(100),
department VARCHAR(100)
)
CREATE TABLE #tblLanguage
(
uniqueid INT,
userid INT,
[language] VARCHAR(100)
)
CREATE TABLE #tblContryPassport
(
uniqueid INT,
userid INT,
contryPassport VARCHAR(100)
)
INSERT INTO #tblUser
VALUES
(1,'aa','TT'),
(2,'bb','gg'),
(3,'cc','rr'),
(4,'dd','dd')
INSERT INTO #tblLanguage
VALUES
(1,1,'American'),
(1,1,'Arabic'),
(1,2,'Azerbaujani'),
(1,3,'Bulgarian'),
(1,3,'Chaochow'),
(1,4,'Behdini')
INSERT INTO #tblContryPassport
VALUES
(1,1,'Hongkong'),
(1,1,'Malaysia'),
(1,2,'Spain'),
(1,3,'China'),
(1,4,'India'),
(1,4,'UK')
Затем объедините уникальный столбец для оси. Если их два, выберите один из них:
DECLARE @cols VARCHAR(MAX),
@colsWithIsNull VARCHAR(MAX)
;WITH CTE
AS
(
SELECT
#tblContryPassport.contryPassport AS Name
FROM
#tblContryPassport
UNION ALL
SELECT
#tblLanguage.[language] AS Name
FROM
#tblLanguage
), CTE2
AS
(
SELECT
ROW_Number() OVER(PARTITION BY name ORDER BY name) AS iRank,
CTE.Name
FROM
CTE
)
SELECT @cols = COALESCE(@cols + ','+QUOTENAME(Name),
QUOTENAME(Name)),
@colsWithIsNull=COALESCE(@colsWithIsNull + ',ISNULL('+QUOTENAME(Name)+',''no'') AS '+QUOTENAME(Name),
'ISNULL('+QUOTENAME(Name)+',''no'') AS '+QUOTENAME(Name))
FROM
CTE2
WHERE
iRank=1
Затем выполните динамический разворот так:
DECLARE @query NVARCHAR(4000)=
N';WITH CTE
AS
(
SELECT
#tblContryPassport.userid,
''yes'' AS Answer,
#tblContryPassport.contryPassport AS Name
FROM
#tblContryPassport
UNION ALL
SELECT
#tblLanguage.userid,
''yes'' AS Answer,
#tblLanguage.[language] AS Name
FROM
#tblLanguage
)
SELECT
userName,
department,
'+@colsWithIsNull+'
FROM
(
SELECT
#tblUser.userid,
#tblUser.userName,
#tblUser.department,
CTE.Answer,
CTE.Name
FROM
#tblUser
JOIN CTE
ON #tblUser.userid=CTE.userid
) AS p
PIVOT
(
MAX(Answer)
FOR Name IN ('+@cols+')
) AS pvt'
EXECUTE(@query)
Тогда в моем случае я опущу временные таблицы:
DROP TABLE #tblUser
DROP TABLE #tblContryPassport
DROP TABLE #tblLanguage