这样写, select count(*) 总人数 , sum(decode(xb,'男',1)) 男性, sum(decode(xb,'女',1)) 男性, sum(decode(yglb,'工人',1)) 工人, sum(decode(yglb,'干部',1)) 干部 from t_ygjbxx SQL SERVER 中用IIF 代替DECODE
select count(aID) "总人数",Sum(Case when sex='男' then 1 end) "男性",Sum(Case When sex='女' then 1 end) "女性",Sum(Case when work='工人' then 1 end) "工人",Sum(Case when Work='干部' then 1 end) "干部" from table
上面的语句不行 SUM好像只能对数值进行统计
ACCESS中好像也有类似的函数,如果还不行,这样写,不用函数: select A.*,B.*,C.*,D.*,E.* FROM (select count(*) 总人数 from t_ygjbxx)A, (select count(*) 男性 from t_ygjbxx where xb='男')B, (select count(*) 女性 from t_ygjbxx where xb='女')C , (select count(*) 干部 from t_ygjbxx where yglb='干部')D, (select count(*) 工人 from t_ygjbxx where yglb='工人')E
这样写,
select
count(*) 总人数 ,
sum(decode(xb,'男',1)) 男性,
sum(decode(xb,'女',1)) 男性,
sum(decode(yglb,'工人',1)) 工人,
sum(decode(yglb,'干部',1)) 干部
from t_ygjbxx
SQL SERVER 中用IIF 代替DECODE
SUM好像只能对数值进行统计
select A.*,B.*,C.*,D.*,E.* FROM
(select count(*) 总人数 from t_ygjbxx)A,
(select count(*) 男性 from t_ygjbxx where xb='男')B,
(select count(*) 女性 from t_ygjbxx where xb='女')C ,
(select count(*) 干部 from t_ygjbxx where yglb='干部')D,
(select count(*) 工人 from t_ygjbxx where yglb='工人')E
[email protected]