| 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.strNotefrom tblM2EvaluationElementReponse as teINNER JOIN tblM2EvaluationElement as teeON tee.intEvaluationElementId = te.intEvaluationElementIdWHERE 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 insteadLest say there 3 questionsName: Q1 Q2 Q3Albert A B DBastarache D A ANotAlbert ABastarache DAlbert BBastarache AAlbert DBastarache AintEvaluationId = id of the testintEvaluationElementId = id for question of the testAny 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 |
 |
|
|
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! |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-28 : 18:46:43
|
| Perhaps your expected output is not clear. |
 |
|
|
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 tables1 : tblM2Evaluation 'That that contain the name of the evaluation2 : tblM2EvaluationElement 'That contain the question3 : tblM2EvaluationElementReponse ' that the score of the student and id of the studentSo a create a query that return all the score for a test for all the studentFor test 1Nameof student Score Name of the questionLuc A Q1Julie C Q1Luc D Q2Julie A Q2My query work fine for that put i'an try to haveName of student Q1 Q2Luc A DJulie C AI hope explain my self write if you need anything else please asikTks in advance! |
 |
|
|
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 @tempSELECT name,[Q1] AS Q1,[Q2] AS Q2FROM (SELECT * FROM @temp) AS PPIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVTuse this dynamic cross tabselect name,max(case when ques = 'q1' then grade end)as ql,max(case when ques = 'q2' then grade end)as q2from @tempgroup by name |
 |
|
|
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 @tempSELECT name,[Q1] AS Q1,[Q2] AS Q2FROM (SELECT * FROM @temp) AS PPIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVTuse this dynamic cross tabselect name,max(case when ques = 'q1' then grade end)as ql,max(case when ques = 'q2' then grade end)as q2from @tempgroup by name
Tks i will try it! |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-29 : 07:59:22
|
| ur welcomeif ur using sql2005, u can use pivot in sql2000, pivot will not work use dynamic cross tab |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2009-01-29 : 08:06:55
|
quote: Originally posted by bklr ur welcomeif ur using sql2005, u can use pivot in sql2000, pivot will not work use dynamic cross tab
Hi it almost working When i useselect * from @tempIt my temp table fine with allo the scoreBut when i useSELECT name,[Q1] AS Q1,[Q2] AS Q2FROM(SELECT * FROM @temp) AS PPIVOT ( max(grade) FOR ques IN ([Q1],[Q2]))AS PVTIt give all the name correctly and Q1 and Q2 put Q1 and Q2 are null any idea thks again!! |
 |
|
|
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 asdeclare @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 useselect * from @tempAlbert 11 dfgfgdfBastarache 22 dfgfgdfBernard 11 dfgfgdfBoudreau 22 dfgfgdfBourque Fearon 11 dfgfgdfSanipass 24 dfgfgdfAlbert 25 tet 2Bastarache 2 tet 2Bernard 2 tet 2Boudreau 3 tet 2Bourque Fearon 4 tet 2Chiasson 5 tet 2Cormier 6 tet 2Cormier 7 tet 2Després 8 tet 2Girouard 9 tet 2Girvan 10 tet 2LeBlanc 11 tet 2Maillet 12 tet 2But if i try the pivotselect * from @tempSELECT strName,[Q1] AS Q1,[Q2] AS Q2,[Q3] AS Q3FROM(SELECT * FROM @temp) AS PPIVOT ( max (strGrade) FOR strQues IN ([Q1],[Q2],[Q3]))AS PVT The result is Albert NULL NULL NULLBastarache NULL NULL NULLBernard NULL NULL NULLBoudreau NULL NULL NULLBourque Fearon NULL NULL NULLChiasson NULL NULL NULLCormier NULL NULL NULLDesprés NULL NULL NULLGirouard NULL NULL NULLGirvan NULL NULL NULLLeBlanc NULL NULL NULLMaillet NULL NULL NULLAny idea why it return null??Tks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-29 : 09:15:24
|
something like:-select * from @tempSELECT strName,[1] AS Q1,[2] AS Q2,[3] AS Q3FROM(SELECT * FROM @temp) AS PPIVOT ( max (strGrade) FOR strQues IN ([1],[2],[3]))AS PVT |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2009-01-29 : 09:18:29
|
quote: Originally posted by visakh16 something like:-select * from @tempSELECT strName,[1] AS Q1,[2] AS Q2,[3] AS Q3FROM(SELECT * FROM @temp) AS PPIVOT ( 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,3Any idea what to try?Tks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-29 : 09:20:54
|
| what are possible values of strQues field? |
 |
|
|
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 studentEx: strQuesAlbert A Question 1Luc A Question 1John B Question 2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
lucsky8
Posting Yak Master
105 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-29 : 09:39:45
|
yup...exactly...you're welcome |
 |
|
|
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 errorMust declare the scalar variable "@temp".I tought i declare the @temp table at the beginTks againdeclare @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 strQuesdeclare @pivot varchar(200), @sum varchar(200), @sql nvarchar(1000), @col intselect @pivot='', @sum=''open cfetch c into @colwhile @@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 endclose cdeallocate cset @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) |
 |
|
|
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 performenceTks for evrything |
 |
|
|
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. |
 |
|
|
|