У меня есть следующий набор данных:
@employees =
SELECT * FROM
( VALUES
("Noah", "cell", "030-0074321"),
("Noah", "office", "030-0076545"),
("Sophia", "cell", "(5) 555-4729"),
("Sophia", "office", "(5) 555-3745"),
("Liam", "cell", "(5) 555-3932"),
("Amy", "cell", "(171) 555-7788"),
("Amy", "office", "(171) 555-6750"),
("Amy", "home", "(425) 555-6238"),
("Justin", "cell", "0921-12 34 65"),
("Justin", "office", "0921-12 34 67"),
("Emma", (string)null, (string)null),
("Jacob", "", ""),
("Olivia", "cell", "88.60.15.31"),
("Olivia", "office", "88.60.15.32"),
("Mason", "cell", "(91) 555 22 82"),
("Mason", "office", "(91) 555 91 99"),
("Mason", "home", "(425) 555-2819"),
("Ava", "cell", "91.24.45.40"),
("Ava", "office", "91.24.45.41"),
("Ethan", "cell", "(604) 555-4729"),
("Ethan", "office", "(604) 555-3745"),
("David", "cell", "(171) 555-1212"),
("Andrew", "cell", "(1) 135-5555"),
("Andrew", "office", "(1) 135-4892"),
("Jennie", "cell", "(5) 555-3392"),
("Jennie", "office", "(5) 555-7293")
) AS T(EmpName, PhoneType, PhoneNumber);
Я пытался использовать пример карты, данный Microsoft ... но я не вижу, как изменить его для генерации столбцов.
@result =
SELECT EmpName,
String.Join(",", MAP_AGG(PhoneType, PhoneNumber).Select(p => String.Format("{0}:{1}", p.Key, p.Value))).Trim() AS PhoneNumbers
FROM @employees
WHERE !string.IsNullOrWhiteSpace(PhoneType)
GROUP BY EmpName;
Какой результат:
Amy cell:(171) 555-7788,home:(425) 555-6238,office:(171) 555-6750
Andrew cell:(1) 135-5555,office:(1) 135-4892
Ava cell:91.24.45.40,office:91.24.45.41
Я хотел бы получить:
User, office, cell, home
Noah, 030-0076545, 030-0074321, null
Sophia, (5) 555-3745, (5) 555-4729, null
Liam, null, (5) 555-3932, null
Amy, (171) 555-6750, (171) 555-7788, (425) 555-6238
...
Заранее спасибо!
V.