用SQL语言从现有的数据库里根据性别属性分别统计出男女相应的一个属性里各类型的数量

作者:佚名    更新日期:2025-06-23
现过几次 2出现过几次
select count(1) from table1 where A=1;
select count(1) from table1 where A=2;
select [1]=count(case when A=1 then 1 else null end),[2]=
(case when A=1 then 1 else null end) from table1
A为1、B为2的情况几次,
select count(1) from table where a=1 and b=2
当A为1,B出现几次 等等
select count(distinct B ) from table1 where A=1

select grider,EnglishEducation,count(*) from table group by grider,EnglishEducation

sql查询一个班级中总共有多少人以及男女分别多少人~

create view StuClassView as
SELECT s.ID ,s.StuName ,s.StuAge ,s.StuAddress ,s.StuTel ,s.ClassId ,s.StuId,s.StuSex ,e.ClassName,e.ClassInfo,e.ClassFlag
FROM Classes as e left join Students as s on s.ClassId=e.ClassIdselect sc.ClassName as '班级名称',count(sc.StuId) as '总人数' ,sum(case when sc.StuSex='男' then 1 else 0 end) as '男', sum(case when sc.StuSex='女' then 1 else 0 end) as '女' from StuClassView as sc group by sc.ClassName!

1、可通过分组和组内计数来实现,语句如下:
select a, count(*) from A Group by a
2、用Group By分组:
Group By + [分组字段](可以有多个)。在执行了这个操作以后,数据集将根据分组字段的值将一个数据集划分成各个不同的小组。
这里,分组字段是a,所以数据集分成了你、我、他三个组。然后用Count(*)分别按照各个组来统计各自的记录数量。
3、Count(*)函数:
Count(*) 函数返回表中的记录数。注意它和Group by连用,返回组内记录数。

扩展资料:
select count(*)和select count(1)的区别
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的。
假如表没有主键(Primary key), 那么count(1)比count(*)快。
如果有主键的话,那主键作为count的条件时候count(主键)最快。
如果你的表只有一个字段的话那count(*)就是最快的。
count(*) 跟 count(1) 的结果一样,都包括对NULL的统计,而count(column) 是不包括NULL的统计。
参考资料:
百度百科.Group by