I have two tables:--StudentGradeif object_id('dbo.StudentGrade') is not null drop table dbo.StudentGrade;create table dbo.StudentGrade( StudentGradeID smallint identity not null, Student varchar(10) not null, Course varchar(10) not null, Credit tinyint not null, GradeNo tinyint not null, Remark varchar(10) null, constraint PK_StudentGrade primary key( StudentGradeID ));insert into dbo.StudentGrade( Student, Course, Credit, GradeNo, Remark)select 'Steve', 'ASM01', 3, 50, null union allselect 'Steve', 'ASM01', 3, 43, null union allselect 'Bob', 'ASM01', 3, 0, null union allselect 'Bob', 'OB01', 3, 23, null union allselect 'Bob', 'OB01', 3, 59, null union allselect 'Bob', 'ASM01', 3, 100, null union allselect 'Andy', 'OB01', 3, 0, null union allselect 'Andy', 'ASM01', 3, 50, null union allselect 'Andy', 'ASM01', 3, 10, null union allselect 'Andy', 'OB01', 3, 70, null union allselect 'Andy', 'OB01', 3, 0, 'ABS';--GradeRateif object_id('dbo.GradeRate') is not null drop table dbo.GradeRate;create table dbo.GradeRate( GradeRateID tinyint identity not null, LowGradeNo tinyint not null, HighGradeNo tinyint not null, Grade varchar(2) not null, Points tinyint not null, constraint PK_GradeRate primary key( GradeRateID ), constraint AK_GradeRate_GradeRange unique( LowGradeNo, HighGradeNo ), constraint AK_GradeRate_Points unique( Points ));insert into dbo.GradeRate( LowGradeNo, HighGradeNo, Grade, Points)select 0, 34, 'F', 0 union allselect 35, 42, 'D', 1 union allselect 43, 50, 'D+', 2 union allselect 51, 58, 'C', 3 union allselect 59, 66, 'C+', 4 union allselect 67, 74, 'B', 5 union allselect 75, 82, 'B+', 6 union allselect 83, 90, 'A', 7 union allselect 91, 100, 'A+', 8;
Now, I created a T-SQL:select Student, Course, Credit, IsAbsent, GradeNo, Grade, Points, Qualityfrom ( select osg.Student, osg.Course, osg.Credit, osg.IsAbsent, osg.GradeNo, case osg.IsAbsent when 1 then 'ABS' else isnull(gr.Grade, 'X') end as Grade, case osg.IsAbsent when 1 then '0' else cast(isnull(gr.Points, 0) as varchar) end as Points, case osg.IsAbsent when 1 then 0 else isnull(gr.Points, 0) end * osg.Credit as Quality from ( select Student, Course, Credit, case Remark when 'ABS' then 1 else 0 end as IsAbsent, (sum(GradeNo) / count(1)) as GradeNo from dbo.StudentGrade group by Student, Course, Credit, case Remark when 'ABS' then 1 else 0 end ) osg left outer join dbo.GradeRate gr on osg.GradeNo between gr.LowGradeNo and gr.HighGradeNo ) CalculatedStudentGradeorder by Student, Coursecompute sum(Credit), sum(Quality);
After running the above T-SQL, am getting the following result:------------------------------------------------------------Student|Course|Credit|IsAbsent|GradeNo|Grade|Points|Quality------------------------------------------------------------Andy ASM01 3 0 30 F 0 0Andy OB01 3 0 35 A 1 3Andy OB01 3 1 0 ABS 0 0Bob ASM01 3 0 50 A 2 6Bob OB01 3 0 41 A 1 3Steve ASM01 3 0 46 A 2 6------------------------------------------------------------
But, now the error is:------------------------------------------------------------Student|Course|Credit|IsAbsent|GradeNo|Grade|Points|Quality------------------------------------------------------------Andy OB01 3 0 35 A 1 3Andy OB01 3 1 0 ABS 0 0------------------------------------------------------------
Whereas it should be come as:------------------------------------------------------------Student|Course|Credit|IsAbsent|GradeNo|Grade|Points|Quality------------------------------------------------------------Andy OB01 3 0 35 A 1 3------------------------------------------------------------
Will you please help me to rectify the erroe, please?Daipayan