我有一个学生平均四个大学年级和领域的表格,用于找到四年制学位的平均值,最后一个领域是用于评分系统。
我的问题:我想根据final_avg
列制作成绩字段值。我怎样才能做到这一点?
例如,如果final_avg < 50
然后grade = failed
,final_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,
)
答案 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)