944,098 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 738
  • MS SQL RSS
Oct 29th, 2009
0

MS SQL: Converting row printing to column based printing

Expand Post »
Hi, I am doing a student progression system where the application is able to keep track of student marks. I have a problem currently and I've been spending nights on this.

Let me briefly explain. Marks are calculated based on their hurdles. So for instance, H1 = xx%, H2 = xx%. The number of hurdles vary for different subjects. The code below is able to provide me the calculated marks based on each individual's hurdles in rows. However, I'm trying to output it in a horizontal format. Meaning:

Instead of:

xx%
xx%

I want it to be: xx% xx% (in two columns)

I've tried using the CASE clause in my SELECT section but that only works if the hurdles are static and fixed. My problem is the hurdles are dynamic as well. Is there anyone who has some idea on how to workaround this issue? Thanks in advance guys!!

MS SQL Syntax (Toggle Plain Text)
  1. SELECT SUM(Marks.marks_value) * Hurdle.hurdle_weight / 100 AS total
  2. FROM Marks INNER JOIN
  3. Student ON Marks.student_id = Student.student_id INNER JOIN
  4. Class ON Student.student_id = Class.student_id INNER JOIN
  5. MarkingCriteria ON Marks.criteria_id = MarkingCriteria.criteria_id INNER JOIN
  6. AssessmentComponent ON MarkingCriteria.component_id = AssessmentComponent.component_id INNER JOIN
  7. Hurdle ON AssessmentComponent.hurdle_id = Hurdle.hurdle_id
  8. GROUP BY Hurdle.hurdle_id, Hurdle.hurdle_weight, Student.student_id, Class.student_id
  9. ORDER BY Hurdle.hurdle_id
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008
Oct 29th, 2009
0
Re: MS SQL: Converting row printing to column based printing
You shouldn't pivot a table (rows -> columns) dynamically. Lets say one person does 10k hurdles where the rest only have done 3... then everyone will have 9k columns of NULLs since they didn't have nearly enough data to pivot. You should probably tackle this issue at the application level and return your data as rows.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 29th, 2009
0
Re: MS SQL: Converting row printing to column based printing
sknake: i am restricting the number of hurdles to 5 at the client level so i'm sure it wouldn't be much of a problem. if i am insisting on my design, how should i make it work? I can't use cross tabs as the hurdle_ids are dynamic.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008
Oct 29th, 2009
1
Re: MS SQL: Converting row printing to column based printing
I posted an example of how to pivot a table in:
http://www.daniweb.com/forums/thread233907.html

I'm afraid your table setup is a little to complex to duplicate on the spot as I don't understand the relationships.

Here is the code from that thread:
MS SQL Syntax (Toggle Plain Text)
  1. IF OBJECT_ID('tempdb..#Parts', 'U') IS NOT NULL DROP TABLE #Parts
  2. CREATE TABLE #Parts
  3. (
  4. ID int,
  5. ID2 int,
  6. PartNo varchar(10)
  7. )
  8. SET NOCOUNT ON
  9. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 23921, 'DENT')
  10. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 26145, 'PLGD')
  11. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 26145, 'PRRP')
  12. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 26145, 'SIST')
  13. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 57290, 'PRMD')
  14. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 57290, 'abc')
  15. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 57290, 'def')
  16. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 57290, 'ghi')
  17. INSERT INTO #Parts (ID, ID2, PartNo) Values (75, 57290, 'jkl')
  18. SET NOCOUNT OFF
  19.  
  20. SELECT ID, ID2,
  21. Max((Case When RowNumber = 1 Then PartNo Else '' End)) AS Part1,
  22. Max((Case When RowNumber = 2 Then PartNo Else '' End)) AS Part2,
  23. Max((Case When RowNumber = 3 Then PartNo Else '' End)) AS Part3,
  24. Max((Case When RowNumber = 4 Then PartNo Else '' End)) AS Part4
  25. FROM
  26. (
  27. SELECT ID, ID2, PartNo,
  28. ROW_NUMBER() OVER(PARTITION BY ID, ID2 ORDER BY PartNo ASC) AS RowNumber
  29. FROM #Parts
  30. ) AS tbl
  31. GROUP BY ID, ID2

Results:
text Syntax (Toggle Plain Text)
  1. ID ID2 Part1 Part2 Part3 Part4
  2. ----------- ----------- ---------- ---------- ---------- ----------
  3. 75 23921 DENT
  4. 75 26145 PLGD PRRP SIST
  5. 75 57290 abc def ghi jkl
  6.  
  7. (3 row(s) affected)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 29th, 2009
0
Re: MS SQL: Converting row printing to column based printing
hmm, let me get this right. First you are creating a temp table to store the data to replicate the problem. For my case, I do not need to create a temp table right?

Next, your insert statements are inserting to the temp table.

And finally, your selecting it out from those table?



MS SQL Syntax (Toggle Plain Text)
  1. SELECT Student_1.student_id, SUM(Marks.marks_value) * Hurdle.hurdle_weight / 100 AS total
  2. FROM Marks INNER JOIN
  3. Student AS Student_1 ON Marks.student_id = Student_1.student_id INNER JOIN
  4. Class AS Class_1 ON Student_1.student_id = Class_1.student_id INNER JOIN
  5. MarkingCriteria ON Marks.criteria_id = MarkingCriteria.criteria_id INNER JOIN
  6. AssessmentComponent ON MarkingCriteria.component_id = AssessmentComponent.component_id INNER JOIN
  7. Hurdle ON AssessmentComponent.hurdle_id = Hurdle.hurdle_id
  8. GROUP BY Hurdle.hurdle_id, Hurdle.hurdle_weight, Student_1.student_id


And my output is this:

1 31.4
1 60
4 0.2
4 39.6


'total ' column is what I am trying to make pivot.
Last edited by dfs3000my; Oct 29th, 2009 at 5:46 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008
Oct 29th, 2009
0
Re: MS SQL: Converting row printing to column based printing
The temp tables are just for simulating data so you could run the solution "as-is" to see how it works. You should only care about the selecting portion.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 29th, 2009
0
Re: MS SQL: Converting row printing to column based printing
I have managed to get the output that I want and below is my code. It may look pretty messy but well, that's the way it is

Thanks sknake for your help!

MS SQL Syntax (Toggle Plain Text)
  1. SELECT student_name, student_number, student_email,
  2. Max((CASE WHEN RowNumber = 1 THEN total ELSE '' END)) AS Hurdle1,
  3. Max((CASE WHEN RowNumber = 2 THEN total ELSE '' END)) AS Hurdle2,
  4. Max((CASE WHEN RowNumber = 3 THEN total ELSE '' END)) AS Hurdle3,
  5. Max((CASE WHEN RowNumber = 4 THEN total ELSE '' END)) AS Hurdle4,
  6. Max((CASE WHEN RowNumber = 5 THEN total ELSE '' END)) AS Hurdle5
  7.  
  8. FROM (SELECT Student.student_id, Student.student_name, Student.student_number, Student.student_email,
  9. ROW_NUMBER() OVER (PARTITION BY Student.student_id ORDER BY Student.student_id) AS RowNumber,
  10. SUM(Marks.marks_value) * Hurdle.hurdle_weight / 100 AS total
  11. FROM Marks INNER JOIN
  12. Student ON Marks.student_id = Student.student_id INNER JOIN
  13. Class ON Student.student_id = Class.student_id INNER JOIN
  14. MarkingCriteria ON Marks.criteria_id = MarkingCriteria.criteria_id INNER JOIN
  15. AssessmentComponent ON MarkingCriteria.component_id = AssessmentComponent.component_id INNER JOIN
  16. Hurdle ON AssessmentComponent.hurdle_id = Hurdle.hurdle_id
  17. GROUP BY Hurdle.hurdle_id, Hurdle.hurdle_weight, Student.student_id, Student.student_name, Student.student_number,
  18. Student.student_email) AS derive
  19.  
  20. GROUP BY student_id, student_name, student_number, student_email

Output:
StudentName StudentNumber StudentEmail Hurdle1 Hurdle2 Hurdle3 Hurdle4 Hurdle5
Adam 0120R22222 adam@hotmail.com 31.5 60 0 0 0
James 4120R22222 james@hotmail.com 0.2 39.6 0 0 0
Last edited by dfs3000my; Oct 29th, 2009 at 6:20 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: View Help
Next Thread in MS SQL Forum Timeline: how to delete duplicate record in a table by using SQL query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC