需求:首先有一张表记录学生姓名、科目和成绩,然后模拟插入几条数据,脚本如下:

create table score
(
       Name     nvarchar(20),--姓名
       subject  varchar(20),--科目
       grade    int--成绩       
);

insert into score(name,subject,grade) values('张三','语文',100);
insert into score(name,subject,grade) values('张三','数学',90);
insert into score(name,subject,grade) values('李四','语文',85);
insert into score(name,subject,grade) values('王五','语文',99);
insert into score(name,subject,grade) values('王五','英语',89);

现在我们需要得到一个结果,能根据姓名分组显示每个学生所参考的科目数量和总分数,期望结果如下:

SQL脚本去重分组统计


那么我们需要写入的sql脚本如下:

select name 姓名, count(distinct subject) 科目, sum(grade) 总分
  from score
 group by name

然后就能得到上面结果了,重点是:count(distinct subject) 科目,再一次显示一下结果视图:

SQL脚本去重分组统计

相关文章:

  • 2021-11-28
  • 2022-03-06
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2021-08-14
猜你喜欢
  • 2022-12-23
  • 2021-06-06
  • 2021-11-17
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案