PersonId EyeColor HairColor FavoriteTVShow FavoriteMovie FavoriteFood BirthCityBirthInfo
---------- ----------- ------------ ------------------- ---------------- --------------- ------------------
A123 Brown Black The Price Is Right NULL The Godfather Pizza New York City
A444 GreenNULL Brown Friends Elf Elf Steak Seattle
PersonId POS ColorInfo FavoriteInfo BirthCityBirthInfo
-------- ----------- --------- ------------------ -------------
A123 1 Brown The Price Is Right New York City
A123 2 Black The Godfather NULL
A123 3 NULL Pizza NULL
A444 1 GreenBrown Friends Seattle
A444 2 NULL Brown Elf NULL
A444 3 NULL Steak NULL
So for each PersonId, each group of columns (2 ColorInfo cols, 3 FavoriteInfo cols, and 1 BirthCityBirthInfo col) is transposed and a position field is addedinto rows. I've tried variations of CROSS APPLY
and UNPIVOT
, but I can't seem to get it just right. I know it's a weird request, but can anyone think of a good way to do this?
--Create the table
CREATE TABLE #test (
PersonId VARCHAR(10),
EyeColor VARCHAR(20),
HairColor VARCHAR(20),
FavoriteTVShow VARCHAR(30),
FavoriteMovie VARCHAR(30),
FavoriteFood VARCHAR(30),
BirthCityBirthInfo VARCHAR(30)
)
--Populate the table
INSERT INTO #test
VALUES
('A123', 'Brown', 'Black', 'The Price Is Right', 'The Godfather'NULL, 'Pizza', 'New York City'),
('A444', 'Green'NULL, 'Brown', 'Friends', 'Elf', 'Steak', 'Seattle')
--See the original result set.
SELECT *
FROM #test
--My desired result set
SELECT *
FROM ( VALUES ('A123', 1, 'Brown', 'The Price Is Right', 'New York City'),
('A123', 2, 'Black', 'The Godfather', NULL),
('A123', 3, NULL, 'Pizza', NULL),
('A444', 1, 'Green''Brown', 'Friends', 'Seattle'),
('A444', 2, 'Brown'NULL, 'Elf', NULL),
('A444', 3, NULL, 'Steak', NULL) ) t (PersonId, POS, ColorInfo, FavoriteInfo, BirthCityBirthInfo)