如何在sql server表创建中进行评分系统

时间:2016-04-30 16:58:53

标签: sql-server

我有一个学生平均四个大学年级和领域的表格,用于找到四年制学位的平均值,最后一个领域是用于评分系统。

我的问题:我想根据final_avg列制作成绩字段值。我怎样才能做到这一点?

例如,如果final_avg < 50然后grade = failedfinal_avg >= 50 and final_avg < 60然后是grade = satisfactory,依此类推。

这是我的表:

CREATE TABLE averages_tbl(
    [avg_id] [int] IDENTITY(1,1) NOT NULL,
    [Student_ID] [int] NOT NULL,
    [f_s_avg] [float] NULL,
    [s_s_avg] [float] NULL,
    [t_s_avg] [float] NULL,
    [fu_s_avg] [float] NULL,
    [final_avg]  AS ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]),
    [grade] [nvarchar](20) NULL,
)

1 个答案:

答案 0 :(得分:1)

简单方法:

CREATE TABLE averages_tbl(
    [avg_id] [int] IDENTITY(1,1) NOT NULL,
    [Student_ID] [int] NOT NULL,
    [f_s_avg] [float] NULL,
    [s_s_avg] [float] NULL,
    [t_s_avg] [float] NULL,
    [fu_s_avg] [float] NULL,
    [final_avg]  AS ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]),
    [grade] AS (CASE WHEN ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]) < 50 THEN 'Failed' 
                     WHEN ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]) between 50 and 59 THEN 'Satisfied'
                     ELSE 'Whatever' END)
)

ALTER TABLE

ALTER TABLE averages_tbl DROP COLUMN [grade]

ALTER TABLE averages_tbl ADD [grade] AS (
    CASE WHEN ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]) < 50 THEN 'Failed' 
         WHEN ((([f_s_avg]+[s_s_avg])+[t_s_avg])+[fu_s_avg]) between 50 and 59 THEN 'Satisfied'
         ELSE 'Passed' END)