Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with getting first value with Pivot

Author  Topic 

Gildebrand
Starting Member

1 Post

Posted - 2010-02-28 : 06:32:46
Hi!

I've written a query that looks like this:
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)


As you can see i use count(Teacher_ID) for the Pivot.
But i would like to get the first found Teacher_ID for each Subject_ID and Elev_ID instead of just getting the count. I am pretty noob so please help :)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-28 : 08:35:37
for that you need to post some sample data and specify on which columns basis you want first record, as without specifying the order in terms of another column, there's no concept of first and last in sql table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -