Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with pivot on course data

$
0
0

Hi, I sincerely hope somebody can help me with this? :-)

This is my first post here so please excuse any mistakes or ambiguities.

I have a query as follows:

SELECT     LOWER(TblPupilManagementPupils.txtUsername) AS username, TblPupilManagementPupils.txtPreName AS firstname, 
                      TblPupilManagementPupils.txtSurname AS lastname, TblPupilManagementPupils.txtEmailAddress AS email, 
                      csm_tblMoodleCourseLink.txtMoodleShortName AS course, csm_tblMoodleCourseLink.txtSetCode AS [group]
FROM         TblPupilManagementPupils INNER JOIN
                      TblTeachingManagerSetLists ON TblPupilManagementPupils.txtSchoolID = TblTeachingManagerSetLists.txtSchoolID INNER JOIN
                      TblTeachingManagerSets ON TblTeachingManagerSetLists.intSetID = TblTeachingManagerSets.TblTeachingManagerSetsID INNER JOIN
                      csm_tblMoodleCourseLink ON TblTeachingManagerSets.txtSetCode = csm_tblMoodleCourseLink.txtSetCode
WHERE     (TblPupilManagementPupils.intSystemStatus = 1) AND (csm_tblMoodleCourseLink.txtMoodleShortName <> N'')
ORDER BY lastname, firstname

This produces a tables as follows (I have removed some columns for the sake of this post)...

username course group

bloggsj Eng1 Eng1a

bloggsj Phys1 Phys1e

bloggsj Fre1 Fre1b

smitha Bio1 Bio1c

perryf Ger3 Ger3b

perryf Lat3 Lat3a

I am looking to convert the data so that each user has only one line...

username course1 group1 course2 group2 course3 group3

bloggsj Eng1 Eng1a Phys1 Phys1e Fre1 Fre1b

smitha Bio1 Bio1c

perryf Ger3 Ger3b Lat3 Lat3a

There is always a group with a course.

Just to add a bit of extra spice, it would be ideal if I didn't have to specify a finite number of courses/groups, e.g. if a user had 20 courses and groups to their name.

If this is a major issue then I'm guessing I could do a count for each username somewhere and find the maximum, then create that many course and group fields?

Again, apologies if there is any required info missing, here's hoping (begging) someone out there can help me :-)

Thanks!




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>