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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Any idea or suggestion

Author  Topic 

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-28 : 14:18:40
Hi,

I have a stored procedure

SELECT     (SELECT stdlastn FROM Bulletin.dbo.tblEleves WHERE stdnumber = te.intElevesId) as strLastName,
(SELECT stdfirstn FROM Bulletin.dbo.tblEleves WHERE stdnumber = te.intElevesId) as strFirstName,
te.strNote
from tblM2EvaluationElementReponse as te
INNER JOIN tblM2EvaluationElement as tee
ON tee.intEvaluationElementId = te.intEvaluationElementId
WHERE tee.intEvaluationId = 22
AND te.strNote <> ''
ORDER BY tee.intEvaluationElementId,strLastName,strFirstName



Albert Natacha a
Bastarache Anika b
Bernard Geneviève c
Boudreau Anabel d
Bourque Ashley a
Chiasson Guillaume b
Cormier James c
Cormier Mireille d
Després Sylvie d
Albert Natacha a
Bastarache Anika a
Bernard Geneviève a
Boudreau Anabel a
Bourque Ashley a
Chiasson Guillaume a
Cormier James a
Cormier Mireille a
Després Sylvie a



As you can see all people are there more then once.Because a test can have multiple question.


My query just look for a certain test ex:
tee.intEvaluationId = 22
This will get all the response for all the question for that test.

Now what i am trying do maybe i need to create a temp table or i have no clue.

I want to have a listing like this instead
Lest say there 3 questions

Name: Q1 Q2 Q3
Albert A B D
Bastarache D A A

Not
Albert A
Bastarache D
Albert B
Bastarache A
Albert D
Bastarache A

intEvaluationId = id of the test
intEvaluationElementId = id for question of the test

Any idea tks in advance!
Sorry for bad english!


revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-01-28 : 15:27:36
Hello;

I think you will want to look at the PARTITION clause of sql for this.

There are some good examples on this forum (its how I learned to use it!). Do a search and see what you come up with.

r&r
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-28 : 17:38:26
quote:
Originally posted by revdnrdy

Hello;

I think you will want to look at the PARTITION clause of sql for this.

There are some good examples on this forum (its how I learned to use it!). Do a search and see what you come up with.

r&r



Hi tks for the reply i will take a look at it!
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-28 : 18:46:43
Perhaps your expected output is not clear.
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 07:46:50
Hi i will try to explain my self beter.
I have a small web appliaction that people can create test for there students.The first create a the test, create the questions and after it finalise the enter the score fot there student.

I have 3 tables


1 : tblM2Evaluation 'That that contain the name of the evaluation
2 : tblM2EvaluationElement 'That contain the question
3 : tblM2EvaluationElementReponse ' that the score of the student and id of the student

So a create a query that return all the score for a test for all the student

For test 1
Nameof student Score Name of the question

Luc A Q1
Julie C Q1
Luc D Q2
Julie A Q2


My query work fine for that put i'an try to have

Name of student Q1 Q2
Luc A D
Julie C A

I hope explain my self write if you need anything else please asik
Tks in advance!
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-29 : 07:54:08
declare @temp table(name varchar(32),grade varchar(4),ques varchar(4))
insert into @temp select 'Luc', 'A', 'Q1' union all select
'Julie', 'C' ,'Q1' union all select
'Luc', 'D' ,'Q2' union all select
'Julie', 'A', 'Q2'

select * from @temp

SELECT name,[Q1] AS Q1,[Q2] AS Q2
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVT

use this dynamic cross tab
select name,max(case when ques = 'q1' then grade end)as ql,max(case when ques = 'q2' then grade end)as q2
from @temp
group by name

Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 07:56:23
quote:
Originally posted by bklr

declare @temp table(name varchar(32),grade varchar(4),ques varchar(4))
insert into @temp select 'Luc', 'A', 'Q1' union all select
'Julie', 'C' ,'Q1' union all select
'Luc', 'D' ,'Q2' union all select
'Julie', 'A', 'Q2'

select * from @temp

SELECT name,[Q1] AS Q1,[Q2] AS Q2
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVT

use this dynamic cross tab
select name,max(case when ques = 'q1' then grade end)as ql,max(case when ques = 'q2' then grade end)as q2
from @temp
group by name






Tks i will try it!
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-29 : 07:59:22
ur welcome
if ur using sql2005, u can use pivot
in sql2000, pivot will not work use dynamic cross tab
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 08:06:55
quote:
Originally posted by bklr

ur welcome
if ur using sql2005, u can use pivot
in sql2000, pivot will not work use dynamic cross tab



Hi it almost working

When i use
select * from @temp
It my temp table fine with allo the score

But when i use


SELECT name,[Q1] AS Q1,[Q2] AS Q2
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVT

It give all the name correctly and Q1 and Q2 put Q1 and Q2 are null any idea thks again!!
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 09:03:31
Hi sorry it the first time i use pivot.

I use this as

declare 
@temp table(
strName varchar(50),
strGrade int,
strQues varchar(50))


insert into @temp
SELECT (SELECT stdlastn FROM Bulletin.dbo.tblEleves WHERE stdnumber = te.intElevesId) as strLastName,
te.strNote,tee.strTitre
from tblM2EvaluationElementReponse as te
INNER JOIN tblM2EvaluationElement as tee
ON tee.intEvaluationElementId = te.intEvaluationElementId
WHERE tee.intEvaluationId = 14
AND te.strNote <> ''
ORDER BY tee.intEvaluationElementId,strLastName


It give me this when i use
select * from @temp

Albert 11 dfgfgdf
Bastarache 22 dfgfgdf
Bernard 11 dfgfgdf
Boudreau 22 dfgfgdf
Bourque Fearon 11 dfgfgdf
Sanipass 24 dfgfgdf
Albert 25 tet 2
Bastarache 2 tet 2
Bernard 2 tet 2
Boudreau 3 tet 2
Bourque Fearon 4 tet 2
Chiasson 5 tet 2
Cormier 6 tet 2
Cormier 7 tet 2
Després 8 tet 2
Girouard 9 tet 2
Girvan 10 tet 2
LeBlanc 11 tet 2
Maillet 12 tet 2

But if i try the pivot

select * from @temp
SELECT strName,[Q1] AS Q1,[Q2] AS Q2,[Q3] AS Q3
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max (strGrade) FOR strQues IN ([Q1],[Q2],[Q3]))AS PVT


The result is
Albert NULL NULL NULL
Bastarache NULL NULL NULL
Bernard NULL NULL NULL
Boudreau NULL NULL NULL
Bourque Fearon NULL NULL NULL
Chiasson NULL NULL NULL
Cormier NULL NULL NULL
Després NULL NULL NULL
Girouard NULL NULL NULL
Girvan NULL NULL NULL
LeBlanc NULL NULL NULL
Maillet NULL NULL NULL

Any idea why it return null??
Tks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 09:15:24
something like:-

select * from @temp
SELECT strName,[1] AS Q1,[2] AS Q2,[3] AS Q3
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max (strGrade) FOR strQues IN ([1],[2],[3]))AS PVT
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 09:18:29
quote:
Originally posted by visakh16

something like:-

select * from @temp
SELECT strName,[1] AS Q1,[2] AS Q2,[3] AS Q3
FROM
(SELECT * FROM @temp) AS P
PIVOT ( max (strGrade) FOR strQues IN ([1],[2],[3]))AS PVT




Hi tks for the quick reply!

I try it put still give null for
1,2,3
Any idea what to try?
Tks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 09:20:54
what are possible values of strQues field?
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 09:22:16
quote:
Originally posted by visakh16

what are possible values of strQues field?



strQuest = 'Name of the question so if could be anything'
strGrade = A,B,C or D the score of the student

Ex: strQues
Albert A Question 1
Luc A Question 1
John B Question 2
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 09:30:16
oh..so that means values are not known before. then you need this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 09:35:59
quote:
Originally posted by visakh16

oh..so that means values are not known before. then you need this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx




OK you write i try
PIVOT ( min (strGrade) FOR strQues IN ([test],[luc],[julie]))AS PVT
instead of
PIVOT ( min (strGrade) FOR strQues IN ([1],[2],[3]))AS PVT

So now i need to do like you said!
Put it dynamic!

tks for your help and time!
Have a nice day!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 09:39:45
yup...exactly...
you're welcome
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 10:11:16
Hi one last faver!

I have this code but it give me this error
Must declare the scalar variable "@temp".
I tought i declare the @temp table at the begin
Tks again


declare 
@temp table(
strName varchar(50),
strGrade varchar(50),
strQues varchar(50))




insert into @temp
SELECT (SELECT stdlastn FROM Bulletin.dbo.tblEleves WHERE stdnumber = te.intElevesId) as strLastName,
te.strNote,tee.intEvaluationElementId
from tblM2EvaluationElementReponse as te
INNER JOIN tblM2EvaluationElement as tee
ON tee.intEvaluationElementId = te.intEvaluationElementId
WHERE tee.intEvaluationId = 14
AND te.strNote <> ''
ORDER BY tee.intEvaluationElementId,strLastName


declare c cursor for select distinct strQues from @temp order by strQues
declare @pivot varchar(200), @sum varchar(200), @sql nvarchar(1000), @col int

select @pivot='', @sum=''

open c
fetch c into @col
while @@FETCH_STATUS = 0
begin
select @sum = @sum + ' SUM(['+ cast(@col as varchar(30)) +']) AS ['+ cast(@col as varchar(30)) +'],',
@pivot = @pivot + ' ['+ cast(@col as varchar(30)) +'], '
fetch c into @col
end
close c
deallocate c

set @sql = 'SELECT strName, '+ left(@sum, len(@sum)-1)+
' FROM @temp PIVOT (Max(strGrade)
FOR strQues IN ('+ left(@pivot, len(@pivot)-1)+ ')) AS Ques
GROUP BY strName'


exec(@sql)
Go to Top of Page

lucsky8
Posting Yak Master

105 Posts

Posted - 2009-01-29 : 10:15:39
Ok sorry it work i use
CREATE TABLE #temp (
strName varchar(50),
strGrade varchar(50),
strQues varchar(50) )

Can someone tell if it ok to use a query like that?
On performence
Tks for evrything
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 10:18:24
no problem thats the way to do...you cant use @ tables inside dynamic sql.
Go to Top of Page
   

- Advertisement -