CREATE TABLE [dbo].[Score](
[ID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nvarchar](50) NULL,
[CID] [int] NULL,
[Score] [int] NULL
)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('张三',1,60)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('张三',2,70)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('张三',3,80)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('张三',4,90)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('李四',1,60)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('李四',2,70)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('李四',3,80)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('李四',4,90)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('王五',1,60)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('王五',2,70)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('王五',3,80)
INSERT INTO [Test].[dbo].[Score]([Name],[CID],[Score])VALUES('王五',4,90)
好了,准备工作做完了,下面我们来写两条Sql语句,解决开始提出的那两个问题
A、统计学生的成绩
select name,SUM(Score)Score from Score group by Name
B、删除表中重复的记录,因为这表中的name是有重复的,所以我们就直接用这表来test
delete from Score where Name in
(select Name from Score group by Name having COUNT(name)>0)and ID
not in (select MIN(id) from Score group by Name having COUNT(Name)>0)
好了,两条语句解决了两个问题.