Please see my SQL questions from the picture above.
how to write the SQL for this?
Please see my SQL questions from the picture above.
how to write the SQL for this?
DECLARE @Columns nvarchar(MAX),@SQL nvarchar(MAX);
SET @Columns=N'';
SELECT @Columns+=IIF(@Columns='',
QUOTENAME(continent),N','+QUOTENAME(continent))
from(Select continent from student group by continent ) as x ;
Set @SQL=N'SELECT ['+STUFF(@Columns,1,1,'')+'
FROM ( Select continent,name,row_number() over(partition by continent order
by name) rn from student ) AS j
PIVOT( MAX( name) FOR continent IN ( ['+STUFF(@Columns,1,1,'')+')) as p';
EXECUTE sp_executesql @SQL;