一个SQL文问题,请教高手

来源:百度知道 编辑:UC知道 时间:2024/09/22 11:35:26
要求查找出学生表中的所有学生,并且统计出每个学生的选课区分为1的个数。请教高手该Sql文如何写?

表名:Students
此表中包含两列:Name和SelectFlg
数据如下:

Students
Name SelectFlg
-------------------
stu1 0
stu1 1
stu1 1
stu2 0
stu2 0
stu3 1
stu3 0

要求实现检索出来以下结果:

Name Count
--------------
stu1 2
stu2 0
stu3 1

select distinct name,isnull((select count(1) from students where selectflg=1 and name=a.name),0) as count
from students a

SELECT Name,Count(SelectFlg) AS SelectFlg FROM Students GROUP BY Name

select max([Name]),count(*) from Students where selectflg=1 group by [Name]

select Name,sum(selectfly) as Count from Students group by Name
试试这个语句.

select name,sum(SelectFlg) as count from students group by name

或者 select name,sum(1) as count from students group by name

select Name,count(SelectFlg) as '总数' from Stundets where Selectflg<>999999 group by Students