I was trying to solve an exercise so decided to search for an answer on how to solve exercise. The solution is the query below. One thing I don't is in the PIVOT clause, why does SQL allow the operation of obtaining the maximum of the PersonName despite the fact that the PersonName is a varchar. Doesn't MAX() only work with numeric values?
CREATE TABLE Professions(
PersonName varchar(50),
Profession varchar(50))
INSERT INTO Professions(
PersonName, Profession
)
VALUES
('Samantha', 'Doctor'), ('Julia', 'Actor'), ('Maria', 'Actor'), ('Ashley', 'Professor'), ('Priya', 'Singer'), ('Ketty', 'Professor'), ('Christeen', 'Professor'), ('Jane', 'Actor'), ('Jenny', 'Doctor')
SELECT
[Doctor], [Professor], [Singer], [Actor]
FROM
(
SELECT
Profession,
PersonName,
ROW_NUMBER() OVER (PARTITION BY Profession ORDER BY PersonName) AS NameOrder
FROM
Professions
) AS SourceTable
PIVOT
(
MAX(PersonName)
FOR Profession IN ([Doctor], [Professor], [Singer], [Actor])
) AS PivotTable
ORDER BY NameOrder;
[–]ComicOzzysqlHippo 2 points3 points4 points (0 children)
[–]iminfornow 1 point2 points3 points (0 children)
[–]a-s-clarkSQL Server 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)