Here's two approach that solve two different problems.
I think you're problem is the second dataset, but I wasn't completely sure.
DECLARE @sourceOne TABLE (id INT, name VARCHAR(30), shoeSize INT)
DECLARE @sourceTwo TABLE (id INT, name VARCHAR(30), shoeSize INT)
DECLARE @sourceThree TABLE (id INT, name VARCHAR(30), shoeSize INT)
INSERT INTO @sourceOne (id, name, shoeSize)
VALUES
(1, 'Patrick', 10),(2, 'noJedi', 4)
INSERT INTO @sourceTwo (id, name, shoeSize)
VALUES
(3, 'Kalman', 10),(4, 'Celko', 2)
INSERT INTO @sourceThree (id, name, shoeSize)
VALUES
(5, 'Erland', 6),(6, 'Naomi ', 8)
SELECT *
FROM @sourceOne
UNION ALL
SELECT *
FROM @sourceTwo
UNION ALL
SELECT *
FROM @sourceThree
DELETE FROM @sourceOne
DELETE FROM @sourceTwo
DELETE FROM @sourceThree
INSERT INTO @sourceOne (id, name, shoeSize)
VALUES
(1, 'Patrick', NULL),(2, NULL, NULL),
(3, NULL, NULL),(4, NULL, 2),
(5, 'Erland', NULL),(6, NULL, 8)
INSERT INTO @sourceTwo (id, name, shoeSize)
VALUES
(1, NULL, 10),(2, 'noJedi', NULL),
(3, NULL, 10),(4, 'Celko', NULL),
(5, NULL, 6),(6, NULL, 8)
INSERT INTO @sourceThree (id, name, shoeSize)
VALUES
(1, NULL, NULL),(2, NULL, 4),
(3, 'Kalman', NULL),(4, NULL, NULL),
(5, NULL, NULL),(6, 'Naomi ', NULL)
SELECT one.id, COALESCE(one.name,two.name,three.name) AS name, COALESCE(one.shoeSize,two.shoeSize,three.shoeSize) AS shoeSize
FROM @sourceOne one
FULL OUTER JOIN @sourceTwo two
ON one.id = two.id
FULL OUTER JOIN @sourceThree three
ON one.id = three.id
No comments:
Post a Comment