| Author |
Topic |
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-10 : 14:37:47
|
| ok here is my problem i have to join 3 tables. stuinfo, grades, courseinfoselect distinct stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student, grades.grade, CourseInfo.hours from grades join stuinfo as stuinfoon (StuInfo.id = grades.id)join CourseInfo as CourseInfoon (grades.courseID = CourseInfo.courseID)something like that my problem is using sum() with a case statementi need grades.grade which are A B C D F to change over to 4 3 2 1 0then add them up for each student when i put my case statement in the select statement it errorssum(Case when grade IS Null Then (case gradeWHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 end)ELSE (Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 End)End) as totalgradethen next i need to pull the course hours for each course and add them up for each student which i have no idea how to dothen i need to totalgrade/totalhours for each student for gpai have no idea what to do on these. i missed a week of school and not getting much helpif you would please help me out and also help me understand id lov ya forever hahaha |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-11-10 : 15:06:34
|
| Have you spoken with your lecturer/teacher/tutor?btw, you don't need 3 case statement, you just need one. Just a SUM(CASE Grade WHEN .... END)Also, lose that DISTINCT. It's probably not necessary, is inefficient and is learning bad practice.--Gail ShawSQL Server MVP |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-10 : 15:38:53
|
| select stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student, grades.grade, CourseInfo.hours,sum(Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 en) as totalgradedoes that look rightfrom grades join stuinfo as stuinfoon (StuInfo.id = grades.id)join CourseInfo as CourseInfoon (grades.courseID = CourseInfo.courseID) |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-10 : 21:21:19
|
If you are using SUM you need to use GROUP BY on the columns in your SELECT which are outside the aggregate function(s)I presume you need the total hours? so SUM(CourseInfo.hours) AS TotalHoursThat leaves these "non aggregate" columns in the select:stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student (I've taken grades.grade out - presumably you don't want that listed, per student, you just want them totalled up as A=4, B=3 etc)So you need to add to the bottom:GROUP BY stuinfo.id, stuinfo.last + ', ' + stuinfo.first to match your SELECT "non aggregate" columns.Thus the SELECT becomesselect stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student,SUM(CourseInfo.hours) AS TotalHours,sum(Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 end) as totalgrade try that and report back with any questions to make sure you've understood it - otherwise you won't be learning it if we do it for you and you don't understand it |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-11 : 12:19:17
|
| so when you use funtions you have to group by the things that arent functions?so my statement would look likeselect stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student,SUM(CourseInfo.hours) AS TotalHours,sum(Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 end) as totalgradefrom grades join stuinfo as stuinfoon (StuInfo.id = grades.id)join CourseInfo as CourseInfoon (grades.courseID = CourseInfo.courseID)GROUP BY stuinfo.id, stuinfo.last + ', ' + stuinfo.firstcool cool so for the gpa would i add in the select statement gpa=totalgrade/totalhours ???? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-11 : 12:24:55
|
| select *, gpa=totalgrade/totalhours from (select stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student,SUM(CourseInfo.hours) AS TotalHours,sum(Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 end) as totalgradefrom grades join stuinfo as stuinfoon (StuInfo.id = grades.id)join CourseInfo as CourseInfoon (grades.courseID = CourseInfo.courseID)GROUP BY stuinfo.id, stuinfo.last + ', ' + stuinfo.first) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-11 : 12:32:38
|
| huh? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-11 : 12:38:49
|
| You can't use totalgrade totalhours in that select statement as thyey are not defined - you would have to repeat the calculation.This is making the select a derived table so that the outer select can reference anything in the inner select result.Try it and seean equivalent statement is;with cte as(select stuinfo.id, stuinfo.last + ', ' + stuinfo.first as Student,SUM(CourseInfo.hours) AS TotalHours,sum(Case grade WHEN 'A' then '4'WHEN 'B' then '3'WHEN 'C' then '2'WHEN 'D' then '1'Else 0 end) as totalgradefrom grades join stuinfo as stuinfoon (StuInfo.id = grades.id)join CourseInfo as CourseInfoon (grades.courseID = CourseInfo.courseID)GROUP BY stuinfo.id, stuinfo.last + ', ' + stuinfo.first)select *, gpa=totalgrade/totalhours from cte==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-11 : 12:41:41
|
| now would that cause gpa to be a seperate table our with it actually add into the table? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-11 : 12:44:22
|
| It's a single resultset.Why not try it.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-11 : 12:47:19
|
| im just trying to understand? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-11 : 13:02:27
|
| try running theseselect *, k=i+j from(select i=1, j=2) aselect i=1, j=2, k=i+j==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sertay
Starting Member
7 Posts |
Posted - 2011-11-29 : 15:43:37
|
| ok nigel thank you so much for your help i just have one more questionWhy is the a needed at the end of the sub statement? I dont understand that part and having trouble finding out information bout it |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-11-29 : 17:44:56
|
its an alias.The more verbose (possibly more readable too) syntax would be:SELECT a.* , a.i + a.j AS kFROM ( SELECT 1 AS i , 2 AS j ) AS a Basically you are giving your derived result set a name (in this case a) with which to refer to it.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|