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 order by in dynamic pivot table

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 this
declare c cursor for select distinct strQues from #temp order by strQues

I would like to have something like this
declare 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,strQues
I will get an error
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

Tks


CREATE 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

Posted - 2012-03-15 : 15:11:14
why do you need cursor for dynamic pivoting? wont this suffice?
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

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

Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2012-03-16 : 07:19:22
Hi visakh16,

Tks for the link i will try it that way today.

Tks for your help


quote:
Originally posted by visakh16

why do you need cursor for dynamic pivoting? wont this suffice?
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

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



Go to Top of Page

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 strQues

So is there a way to that here?
Tks again

'Here where i think i can achive that
SELECT @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 rank
from 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 = 1202
ORDER BY te.schoolid,te.stdlastn,te.stdfirstn,ts.Affichage,tob.Affichage,td.Affichage



DECLARE @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
)t
PIVOT (MIN(strNote) FOR strQues
IN ('+@question+')) AS pvt'

EXECUTE (@query)


drop table #temp
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 question
rank the order i would like
I 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 one

Any idea? i hope i explain my self right

T2 = score
ID = question

Part of the result from query
strLastName strFirstName T2 ID rank
xxx xxx B 4388 1
xxx xxx B 4755 2
xxx xxx B 4389 3
xxx xxx B 4756 4
xxx xxx B 4757 5
xxx xxx A 4390 6
xxx xxx B 4758 7

The the result from my query with the pivot:

xxx xxx B B A A B B B

The result i want with the pivot

xxx xxx B B B B B A B


quote:
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 MVP
http://visakhm.blogspot.com/



Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.Affichage
That what they use on the other application to know witch order they want

So that why i create d
rank() over (order by tob.id) as rank
i tought i could use for the order by with my pivot
it 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 MVP
http://visakhm.blogspot.com/



Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2012-03-16 : 14:53:39
yes
do you mean something like this??
I know this code want work
but 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, '') + ']'


Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2012-03-19 : 07:27:33
Sorry i am having hard time
I can't not just to this?
Sorry
strQues is the question that i need to pivot
intRank the one i want to order by

If order by intRank i will get an error ORDER BY items must appear in the select list if SELECT DISTINCT is specified

Tks

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 MVP
http://visakhm.blogspot.com/



Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2012-03-19 : 07:55:56
Ok i think i finnally got it?
it working
is it ok this way??

Many Many tks for your help and patient!!!

I change the Group by instead of DISTINCT

quote:

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, '') + ']'

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 08:56:44
yep...looks fine

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

Go to Top of Page
   

- Advertisement -