你知道我如何连接这些列而不重复的值,假设我有一个表的结构(外部连接的结果):
ColumnA ColumnB
--------------------
12345 null
69875 null
null 15648
76582 76582
null 29829
35731 35731
期望输出:
ColumnC
-------
12345
69875
15648
76582
29829
35731
这是我能想到的最好的方法
DECLARE @ColumnA TABLE (val INT)
INSERT INTO @ColumnA VALUES (12345)
INSERT INTO @ColumnA VALUES (69875)
INSERT INTO @ColumnA VALUES (NULL)
INSERT INTO @ColumnA VALUES (76582)
INSERT INTO @ColumnA VALUES (NULL)
INSERT INTO @ColumnA VALUES (35731)
DECLARE @ColumnB TABLE (val INT)
INSERT INTO @ColumnB VALUES (NULL)
INSERT INTO @ColumnB VALUES (NULL)
INSERT INTO @ColumnB VALUES (15648)
INSERT INTO @ColumnB VALUES (76582)
INSERT INTO @ColumnB VALUES (29829)
INSERT INTO @ColumnB VALUES (35731)
select
IIF (colA.val = colB.val, CAST(colA.val AS VARCHAR), ISNULL(CAST(colA.val AS VARCHAR), '') + ISNULL(CAST(colB.val AS VARCHAR), '')) concatenado
from (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) nro, val
FROM @ColumnA
) colA
JOIN (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) nro, val
FROM @ColumnB
) colB ON colA.nro = colB.nro