| Author |
Topic |
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-15 : 09:00:27
|
Hi, pretty new to pivot table.I am using a dynamic pivot.Evrything work fine except i want to my pivot to use intPostion for my order by instead of strQues!Right now i have thisdeclare c cursor for select distinct strQues from #temp order by strQuesI would like to have something like thisdeclare c cursor for select distinct strQues from #temp order by intPosition Do you think there a way to achive that?If i try this : declare c cursor for select distinct strQues,intPosition from #temp order by intPosition,strQuesI will get an errorCursorfetch: The number of variables declared in the INTO list must match that of selected columns.TksCREATE TABLE #temp ( strLastName varchar(200), strFirstName varchar(200), strNote varchar(200), strQues varchar(200), intPosition INT ) insert into #temp SELECT te.stdlastn as strLastName, te.stdfirstn as strFirstName, tr.T2, td.ID, --that i will use for the groupe by rank() over (order by tob.id) as rank --Order i want the pivot table to in from Bulletin.dbo.tblEleves as te INNER JOIN tblBulletin as tb ON te.stdnumber=tb.NoEtud INNER JOIN tblNomBull as tnb ON tb.NoBull=tnb.ID INNER JOIN tblSection as ts ON ts.ID_Bull = tnb.ID INNER JOIN tblObjet as tob ON tob.ID_Sec = ts.ID INNER JOIN tblDescripteur as td ON td.ID_Obj = tob.ID INNER JOIN tblReponse as tr ON tr.ID = tb.ID where intActif = 1 and tb.NoBull not like '%EdPhy%' and tnb.Nom not like '%EdPhy%' and tnb.Date = '2011-2012' and tr.[Desc] = td.ID and te.stdGradeId = 4 and ts.Nom = 'français' and te.schoolid = 1202 and td.Nom <> 'L’élève' ORDER BY te.schoolid,te.stdlastn,te.stdfirstn,ts.Affichage,tob.Affichage,td.Affichage --select distinct strQues,intRank from #temp order by intPosition declare c cursor for select distinct strQues from #temp order by strQues declare @pivot varchar(1000), @sum varchar(1000), @sql nvarchar(1000), @col int select @pivot='', @sum='' open c fetch c into @col while @@FETCH_STATUS = 0 begin select @sum = @sum + ' MAX(['+ cast(@col as varchar(200)) +']) AS ['+ cast(@col as varchar(200)) +'],', @pivot = @pivot + ' ['+ cast(@col as varchar(200)) +'], ' fetch c into @col end close c deallocate c set @sql = 'SELECT strLastName,strFirstName,'+ left(@sum, len(@sum)-1)+ ' FROM #temp PIVOT (Max(strNote) FOR strQues IN ('+ left(@pivot, len(@pivot)-1)+ ')) AS Ques GROUP BY strFirstName,strLastName ORDER BY strLastName,strFirstName' exec(@sql) drop table #temp |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
lucsky8
Posting Yak Master
105 Posts |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-16 : 10:41:53
|
Ok so i change my code to :Is it better this way?But i still stuck on the same part i want my pivot to order by intRank and not by strQuesSo is there a way to that here?Tks again'Here where i think i can achive thatSELECT @question = STUFF(( SELECT DISTINCT '],[' + ltrim(str(strQues)) FROM #temp ORDER BY '],[' + ltrim(str((strQues))) FOR XML PATH('') ), 1, 2, '') + ']'CREATE TABLE #temp ( strLastName varchar(200), strFirstName varchar(200), strNote varchar(200), strQues varchar(200), intRank int)insert into #temp SELECT te.stdlastn as strLastName, te.stdfirstn as strFirstName, tr.T2,td.ID, rank() over (order by tr.num) as rankfrom Bulletin.dbo.tblEleves as te INNER JOIN Bulletin.dbo.tblBulletin as tb ON te.stdnumber=tb.NoEtud INNER JOIN Bulletin.dbo.tblNomBull as tnb ON tb.NoBull=tnb.ID INNER JOIN Bulletin.dbo.tblSection as ts ON ts.ID_Bull = tnb.ID INNER JOIN Bulletin.dbo.tblObjet as tob ON tob.ID_Sec = ts.ID INNER JOIN Bulletin.dbo.tblDescripteur as td ON td.ID_Obj = tob.ID INNER JOIN Bulletin.dbo.tblReponse as tr ON tr.ID = tb.ID where intActif = 1 and tb.NoBull not like '%EdPhy%' and tnb.Nom not like '%EdPhy%' and tnb.Date = '2011-2012' --and tb.Niveau = 'DEUXIÈME année' --and te.schoolid = @intEcoleId and tr.[Desc] = td.ID and te.stdGradeId = 4 and ts.Nom = 'français' and te.schoolid = 1202ORDER BY te.schoolid,te.stdlastn,te.stdfirstn,ts.Affichage,tob.Affichage,td.AffichageDECLARE @query VARCHAR(4000)DECLARE @question VARCHAR(2000)SELECT @question = STUFF(( SELECT DISTINCT '],[' + ltrim(str(strQues)) FROM #temp ORDER BY '],[' + ltrim(str((strQues))) FOR XML PATH('') ), 1, 2, '') + ']'SET @query ='SELECT * FROM( SELECT strLastName,strFirstName,strQues,strNote FROM #temp)tPIVOT (MIN(strNote) FOR strQuesIN ('+@question+')) AS pvt'EXECUTE (@query)drop table #temp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-16 : 11:02:51
|
| why are concerned on order of sequence of columns? you can always return them in order you want at frond end application------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-16 : 11:57:53
|
it would be great but i don't think i can do it or should i say i no idea how.The ID is the id of the questionrank the order i would likeI want that order because the user will compare score that are shown in another application in that perticuler order.There is more the on person (xxx) in the list but for the example i only but oneAny idea? i hope i explain my self rightT2 = scoreID = questionPart of the result from querystrLastName strFirstName T2 ID rankxxx xxx B 4388 1xxx xxx B 4755 2xxx xxx B 4389 3xxx xxx B 4756 4xxx xxx B 4757 5xxx xxx A 4390 6xxx xxx B 4758 7The the result from my query with the pivot: xxx xxx B B A A B B B The result i want with the pivotxxx xxx B B B B B A Bquote: Originally posted by visakh16 why are concerned on order of sequence of columns? you can always return them in order you want at frond end application------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-16 : 12:19:31
|
| while pivoting are you generating row list based on order of rank?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-16 : 12:28:28
|
Base on the question 'strQues'The reason the order is complicated it because it comming from an other database and is made of 3 cretiria for the order.ts.Affichage,tob.Affichage,td.AffichageThat what they use on the other application to know witch order they wantSo that why i create drank() over (order by tob.id) as rank i tought i could use for the order by with my pivotit a row that auto increment My query already in the order i want put when i use the pivot it will be in the order strQues but i would like to use rank Sorry if i have hard time explaning my self big tks for trying to help me!quote: Originally posted by visakh16 while pivoting are you generating row list based on order of rank?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-16 : 14:48:52
|
| you need to apply order by in statement where you generate columnlist (@question) for the pivot------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-16 : 14:53:39
|
yesdo you mean something like this??I know this code want workbut is there a way to make it work?SELECT @question = STUFF(( SELECT DISTINCT '],[' + ltrim(str(strQues)) FROM #temp ORDER BY '],[' + ltrim(str((intRank))) FOR XML PATH('') ), 1, 2, '') + ']' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-16 : 14:56:40
|
| yup, but make it numeric based dont do the convert------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-19 : 07:27:33
|
Sorry i am having hard timeI can't not just to this?SorrystrQues is the question that i need to pivotintRank the one i want to order byIf order by intRank i will get an error ORDER BY items must appear in the select list if SELECT DISTINCT is specifiedTks quote: SELECT @question = STUFF(( SELECT DISTINCT '],[' + strQues FROM #temp ORDER BY '],[' + intRank FOR XML PATH('') ), 1, 2, '') + ']'
quote: Originally posted by visakh16 yup, but make it numeric based dont do the convert------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2012-03-19 : 07:55:56
|
Ok i think i finnally got it?it workingis it ok this way??Many Many tks for your help and patient!!!I change the Group by instead of DISTINCTquote: SELECT @question = STUFF(( SELECT '],[' + ltrim(str(strQues)) FROM #temp GROUP BY '],[' + ltrim(str(strQues)) ORDER BY '' + convert(int,min(intRank)) FOR XML PATH('') ), 1, 2, '') + ']'
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-19 : 08:56:44
|
| yep...looks fine------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|