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.
| Author |
Topic |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2010-04-21 : 13:27:51
|
| I have the following tables:assign table (assignID, assignName, points)table assignpoint table (assignid, studentid, score)I want to display them in table format as followsstudentid ,assginname1, assignname2, assigname3, etc. total1 , 90, 80, 90, etc. How can I do that I know about pivot tables but i can not implement it in my senario mentioned above.I am using SQL server 2008 and coldfusionI want to create a stored procedure that will produce the following result set in the format mentioned beforesarah |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-04-21 : 13:51:30
|
| But why you don't want to use pivot ?I know there must be reason but if you tell us the reason we may give better suggestions.Example using pivot:Create table #Assign(assignID Int, assignName Varchar(255), points int )Create table #assignpoint(assignID Int, studentid int, score int )Insert into #AssignSelect 1,'assginname1',10 unionSelect 2,'assginname2',10 unionSelect 3,'assginname3',10 Insert into #assignpointSelect 1,1, 5 unionSelect 2,1, 8 unionSelect 3,1, 3 unionSelect 1,2, 8 unionSelect 2,2, 9 unionSelect 2,3, 6 Select * from(Select Ap.StudentId, A.AssignId, Ap.Score,Ap.StudentId as Stud1from #assignpoint ap inner join #Assign Aon Ap.assignID = a.assignID) pPivot(Sum(Score)FOR AssignId IN ([1],[2],[3])) AS PVTORDER BY 1You can also refer the below link on using dynamic pivot:http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspxRegards,Bohra |
 |
|
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2010-04-21 : 14:37:09
|
| ThanksBut I do not know the number of assignments before hand so I can not say [1], [2],[3]Can you help me with thatsarah |
 |
|
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2010-04-21 : 14:42:04
|
| ThanksI am reading about dynamic pivot nowsarah |
 |
|
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2010-04-21 : 14:45:04
|
| Is it possible to edit the tables underneath the pivoting process using stored proceduresarah |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-21 : 23:35:44
|
something like:-DECLARE @AssignmentList varchar(max),@SQL varchar(8000)SELECT @AssignmentList='[' + STUFF((SELECT '],['+ assignName FROM assign FOR XML PATH('')),1,3,'') + ']'SET @SQL='SELECT studentid,' + @AssignmentList + ' FROM ( SELECT ap.studentid,ap.score,a.assignName FROM assignpoint ap JOIN assign a ON a.assignID=ap.assignID )m PIVOT (SUM(Score) FOR assignName IN (' + @AssignmentList + ')p'EXEC (@SQL)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|