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 With Pivot Command

Author  Topic 

c9dw2rm8
Starting Member

2 Posts

Posted - 2010-05-14 : 08:27:03
Hi,
I need to convert this Access SQL command to SQL SERVER COMMAND - I try to llok for examples in the Internet but non of them
had the same logic as here



"TRANSFORM First(tblStudentsMeetingsRelation.studentStatus) AS FirstOfstudentStatus
SELECT tblStudentsMeetingsRelation.studentId, tblStudents.studentFirstName, tblStudents.studentLastName
FROM tblStudents INNER JOIN tblStudentsMeetingsRelation ON tblStudents.studentId = tblStudentsMeetingsRelation.studentId
WHERE (((tblStudentsMeetingsRelation.studentDeptId)="372") AND ((tblStudentsMeetingsRelation.studentSeminarId)="372-3-3455-B"))
GROUP BY tblStudentsMeetingsRelation.studentId, tblStudents.studentFirstName, tblStudents.studentLastName
PIVOT tblStudentsMeetingsRelation.studentMeetingId;"



I will glad if Someone will help me with that


This is the Originl Table :
studentId | studentDeptId | studentSeminarId | studentMeetingId | studentStatus
----------------------------------------------------------------------------------------
40223059 | 372 | 372-3-3455-B | 372-1-05112010 | A
40223059 | 372 | 372-3-3455-B | 372-2-05112010 | DDD
40223059 | 372 | 372-3-3455-B | 372-3-05112010 | C
43162007 | 372 | 372-3-3455-B | 372-1-05112010 | A
43162007 | 372 | 372-3-3455-B | 372-2-05112010 | B
43162007 | 372 | 372-3-3455-B | 372-3-05112010 | C


And the result i'm looking for look like this:

studentId | studentFirstName | studentLastName | 372-1-05112010 | 372-2-05112010 | 372-3-05112010
----------------------------------------------------------------------------------------------------------------------
40223059 | ???? | ???? | A | DDD | C
43162007 | ??? | ?????? | A | B | C

* Sorry for the English

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 10:21:13
[code]select studentId
, null as studentFirstName
, null as studentLastName
, max(case when studentMeetingId = '372-1-05112010' then studentStatus else null end) as [372-1-05112010]
, max(case when studentMeetingId = '372-2-05112010' then studentStatus else null end) as [372-2-05112010]
, max(case when studentMeetingId = '372-3-05112010' then studentStatus else null end) as [372-3-05112010]
from StudentTable
group by studentId[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-14 : 12:54:22
or use pivot if using sql 2005 or more

SELECT studentId, studentFirstName , studentLastName,[372-1-05112010],[372-2-05112010],[372-3-05112010]
FROM (SELECT studentId, studentFirstName , studentLastName , studentMeetingId, studentStatus
FROM Table)t
PIVOT (MAX(studentStatus) FOR studentMeetingId IN ([372-1-05112010],[372-2-05112010],[372-3-05112010]))p


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

Go to Top of Page

c9dw2rm8
Starting Member

2 Posts

Posted - 2010-05-23 : 17:51:34
quote:
Originally posted by visakh16

or use pivot if using sql 2005 or more

SELECT studentId, studentFirstName , studentLastName,[372-1-05112010],[372-2-05112010],[372-3-05112010]
FROM (SELECT studentId, studentFirstName , studentLastName , studentMeetingId, studentStatus
FROM Table)t
PIVOT (MAX(studentStatus) FOR studentMeetingId IN ([372-1-05112010],[372-2-05112010],[372-3-05112010]))p


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





Thanks, But the Problem is that the meeting ID are Dynamic and always change , it's not always these [372-2-05112010],[372-3-05112010].
You have something else ?

*sorry for the english
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2010-05-23 : 18:32:42
Search for Dynamic Pivot in this forum. There are plenty of posts for it.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-24 : 09:42:57
Use this
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -