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 '25-30歲' as 年齡段 count(*) as 人數 from tb where year(getdate())-year(birthday)>=25 and year(getdate())-year(birthday)<30union allselect '30-35歲' as 年齡段 count(*) as 人數 from tb where year(getdate())-year(birthday)>=30 and year(getdate())-year(birthday)<35union allselect '35-40歲' as 年齡段 count(*) as 人數 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
*/