select case when xb=1 then '男' else '女' end ,count(*) from student group by xb
select case when xb=1 then '男' else '女' end xb, count(*) num from student group by xb
select a.sex ,count(b.*) from xbdm a join student b on a.sexid=b.sexid group by a.sexid
select b.性别描述字段,count(*) from student a join 性别代码表 b on a.xb=b.xb group by b.性别描述字段
declare @t table (a varchar(10),b int) insert @t select '1',20 union all select '1',30 union all select '1',40 union all select '1',100 union all select '2',20 union all select '2',30 union all select '2',40select * from @T select case when a=1 then '男' else '女' end as 性别,count(b) as 次数 from @t Group by a/* 性别 次数 ------------------------- 男 4 女 3 */
select case when xb=1 then '男' else '女' end ,sum(1) from student group by xb
select b.name,count(*) from Student a,xbdm b where a.id=b.id group by b.name
--既然你有xbdm表,就用:select b.性别名称,人数=count(*) from Student a join xbdm b on a.xb=b.xb group by a.xb
--如果性别的名称是固定的,就男和女,你可以用:select 性别=substring('男女',性别,1),count(*) from student group by xb
case when xb=1 then '男' else '女' end xb,
count(*) num from student group by xb
from xbdm a join student b on a.sexid=b.sexid
group by a.sexid
from student a
join 性别代码表 b on a.xb=b.xb
group by b.性别描述字段
insert @t
select '1',20
union all select '1',30
union all select '1',40
union all select '1',100
union all select '2',20
union all select '2',30
union all select '2',40select * from @T
select case when a=1 then '男' else '女' end as 性别,count(b) as 次数 from @t
Group by a/*
性别 次数
-------------------------
男 4
女 3
*/
else '女' end ,sum(1) from student group by xb
group by a.xb
虽然sdhdy(大江东去...) 、zjcxc(邹建) 的语句在上述例子中可以实现
但是不能推广于其它范例
比如代码表中有两个记录其名称一样但是代码不一样,他们的语句区分不出来而 gmlxf(烛光)
的语句则可以推广到任何情况(至少我没有测出来有什么不妥)另外,由于这几天出差在外,所以给分晚了,希望各位大侠见谅