SQL查询统计某表的男女各个人数
select *,count(*) from student s GROUP BY sex; GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
测试student表纪录如下图,根据自己需求增删字段。 统计男女人数sql如下图: student s ,s是自己为student表定义的别名,count()为统计的人数。
拓展资料: SQL GROUP BY 语法: SELECT column_name(列名), aggregate_function(column_name) (函数名) FROM table_name(表名) WHERE column_name operator value GROUP BY column_name。
sql语句统计数量,统计一个字段的值的数量
select type,count(*) as 总数量,
sum(case when level=’一级’ then 1 else 0 end) as 一级,
sum(case when level=’二级’ then 1 else 0 end) as 二级,
sum(case when level=’三级’ then 1 else 0 end) as 三级
from table group by type
楼上的应该改改吧
利用SQL语句统计出各年龄段人数
select ‘2530岁’ as 年龄段 count(*) as 人数 from tb where year(getdate())year(birthday)>=25 and year(getdate())year(birthday)<30union allselect '3035岁' as 年龄段 count(*) 人数 from tb where year(getdate())year(birthday)>=30 and year(getdate())year(birthday)<35union allselect '3540岁' as 年龄段 count(*) 人数 from tb where year(getdate())year(birthday)>=35 and year(getdate())year(birthday)<40。
求教一个统计数量的sql语句
create table 订单表 (
定单号 varchar(10), 状态 int)
insert 订单表 select
‘DD1001’ , 1 union select
‘DD1002’ , 1 union select
‘DD1003’ , 1 union select
‘DD1004’ , 0 union select
‘DD1005’ , 0 union select
‘DD1006’ , 1
go
create table 订单详细表 (
定单号 varchar(10), 物品名 varchar(10), 数量 int)
insert 订单详细表 select
‘DD1001’ , ‘语文书’ , 30 union select
‘DD1001’ , ‘数学书’ , 20 union select
‘DD1001’ , ‘电脑书’ , 30 union select
‘DD1002’ , ‘电脑书’ , 10 union select
‘DD1003’ , ‘政治书’ , 30 union select
‘DD1003’ , ‘电脑书’ , 15 union select
‘DD1003’ , ‘英语书’ , 10 union select
‘DD1003’ , ‘语文书’ , 6 union select
‘DD1004’ , ‘电脑书’ , 5 union select
‘DD1004’ , ‘语文书’ , 8 union select
‘DD1005′ ,’杂志’ ,12 union select
‘DD1005’ , ‘小说’ ,14 union select
‘DD1005’ , ‘电脑书’ , 15 union select
‘DD1005’ , ‘语文书’ , 21 union select
‘DD1005’ , ‘化学书’ , 18 union select
‘DD1006’ , ‘物理书’ , 22 union select
‘DD1006’ , ‘化学书’ , 58union select
‘DD1007’ , ‘化学书’ , 20
go
select 物品名,SUM(数量) as 数量
from 订单详细表 a join 订单表 b on a.定单号=b.定单号
where b.状态 = 1
group by 物品名
/*
物品名 数量
电脑书 55
化学书 58
数学书 20
物理书 22
英语书 10
语文书 36
政治书 30
*/