简体   繁体   中英

Add new row as Total Grade in

I want a list of a student's transcript in the last row (total score) as the total grade point average

For example:

Course Grade
Math 15
Physics 19
Chemistry 14
Total Average 16

But I can't make the last row (Total Average) Is there a way to create a virtual row?

select crs.Name,
Grade,
crs.CourseTypeId
from Student std
    inner join Selection sct on sct.StudentId = std.Id
    inner join Major mjr on mjr.Id = std.MajorId
    inner join CourseMajor CrsM on CrsM.MajorId = mjr.Id
    inner join Course crs on crs.Id = Crsh.CourseId
    inner join Teach tch on tch.CourseId = crs.Id
    inner join Teacher tchr on tchr.Id = tch.TeacherId
    inner join Unit unt on unt.TeachId = tchr.Id
    inner join Term trm on trm.Id = unt.TermId
where std.FirstName = N'sara' 
    and TermId = EnterTermId
order by crs.Name

Yes you can just use:

(select(SUM(50/5))) as 'AVG1'
(select(AVG(50/5))) as 'AVG2'

as a new column,

select tbl_Test.id2, tbl_Test.sName
    , (select(SUM(50/5))) as 'AVG'
from tbl_Test;

Update: Create a new table and set the calculation and the name, mine I created tbl_result to store result as caption and totalAverage as a decimal to store the calculation AVG, SUM, etc. The best way to implement this is by using Stored Procedure here you can create a virtual table as you say that.

select * from tbl_result;
-- Insert your cal. here
insert into tbl_result (idr, result, totalAverage ) values (1, 'Total Average', (select(AVG(50/5))))

-- Get the result and combine with your select as 'Virtual row' :)
select tbl_Test.id2, tbl_Test.sName from tbl_Test
union
select tbl_result.totalAverage, tbl_result.result from tbl_result

The final result: 结果

Here is how you can do it using Stored Procedure SP:

USE [Testing]
GO
/****** Object:  StoredProcedure [dbo].[sp_Result]    Script Date: 3/19/2022 10:46:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_Result]

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @tempTable AS TABLE (rid INT, result nvarchar(50), totalAverage decimal(18,4))
    DECLARE @cal as decimal(18,4)

    -- Get your cal. avg, sum, count ....
    select @cal = AVG(tbl_Test.id2) from tbl_Test
    -- save the result....
    insert into @tempTable (rid, result, totalAverage) values (1, 'Total Average', @cal)

    -- Return the final result: 
    select tbl_Test.id2, tbl_Test.sName from tbl_Test
    union
    select totalAverage, result from @tempTable


END

How to run SP? -- Execute the SP exec sp_Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM