Hi!

I am running SQL Server 2008. I've written a SQL-Query, that is pivoting the data in my table IUP.
Here is the query
Code:
declare @listcol nvarchar(max)
declare @query nvarchar(max)
select @listcol = STUFF(( Select distinct 
'],[' + LTRIM(str(Subject_ID))
from IUP i
left join Subjects S
on i.Subject_ID=S.ID
FOR XML PATH('')
), 1, 2, '') + ']'

set @query =
'SELECT * 
FROM (  
SELECT Elev_ID, Subject_ID, Teacher_ID  
FROM IUP) myTemp  
PIVOT (count(Teacher_ID) FOR Subject_ID 
IN (' +@listcol+')) AS pvt
order by Elev_ID'
execute (@query)
The result:
Code:
Elev_ID	10	13	14	15	16	2	3	4	6
0687B0C9-9454-4DE5-AF41-077EB10BCD81	0	0	1	0	0	0	0	0	0
B4BEB520-5F57-4B31-9DD4-0F580B84B752	0	0	0	0	0	0	0	0	0
8F34753E-FE45-40AD-B51A-4A5C493B494C	0	0	0	0	0	0	0	0	0
Now this gives me a number of teachers for each student and subject. But how could i get the first found teacher_id instead of counting the number of teachers?
You can see that in my query, i've added a leftjoin that im currently not using. I would like to know how i could present S.Subject instead of the Subject_ID.