求教各位大神啊!我想对表里的部分数据进行分组
例如:
--给个例子参考
CREATE TABLE StudentGrade(
stuId CHAR(4), --学号
subId INT, --课程号
grade INT, --成绩
PRIMARY KEY (stuId,subId)
)
GO
--表中数据如下
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',1,97);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',2,50);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',3,70);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',1,92);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',2,80);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',3,30);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',1,93);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',2,95);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',3,85);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '004 ',1,73);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '004 ',2,78);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '004 ',3,87);
GO
这时,查询结果是:
stuId subId grade
001 1 97
001 2 50
001 3 70
002 1 92
002 2 80
002 3 30
003 1 93
003 2 95
003 3 85
004 1 73
004 2 78
004 3 87
现在我想对stuId=‘001’的grade进行求和,也就是要得到结果是:
stuId subId grade
001 0 217
002 1 92
002 2 80
002 3 30
003 1 93
003 2 95
003 3 85
004 1 73
004 2 78
004 3 87
而我由于实际情况数据量大,不想用union实现,想请教各位大神能在一个select语句中实现吗?
谢谢!情况紧急!在线等啊!
------解决方案--------------------
这样吗:
--给个例子参考
CREATE TABLE StudentGrade(
stuId CHAR(4), --学号
subId INT, --课程号
grade INT, --成绩
PRIMARY KEY (stuId,subId)
)
GO
--表中数据如下
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',1,97);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',2,50);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '001 ',3,70);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',1,92);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',2,80);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '002 ',3,30);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',1,93);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',2,95);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '003 ',3,85);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES( '004 ',1,73);