假设一个表
country peoplename sex
村1 某甲 男
村2 某乙 女
村3 某丙 男
村2 张三 男
村3 李四 其他
村3 王五 女select count(*) from table group by country,sex有没有办法返回的是
村1 男 1
村1 女 0
村1 其他 0
村2 男 1
村2 女 1
村2 其他 0
村3 男 1
村3 女 1
村3 其他 1谢谢。
country peoplename sex
村1 某甲 男
村2 某乙 女
村3 某丙 男
村2 张三 男
村3 李四 其他
村3 王五 女select count(*) from table group by country,sex有没有办法返回的是
村1 男 1
村1 女 0
村1 其他 0
村2 男 1
村2 女 1
村2 其他 0
村3 男 1
村3 女 1
村3 其他 1谢谢。
from
(select distinct country from [table]) a join (select sex='男' union select '女' union select '其它') b on 1=1
left join table c on a.country=c.country and b.sex=c.sex
group by a.country,b.sex
(
country nvarchar(10),
sex nvarchar(10),
)insert into t1
select '村1','男' union all
select '村2','女' union all
select '村3','男' union all
select '村2','男' union all
select '村3','其他' union all
select '村3','女'select A.country,B.sex,ISNULL(C.total,0) as total
from
(select distinct country from t1) A
join (select distinct sex from t1) B on 1=1
left join (select country,sex,count (*) as total from t1 group by country,sex) C on A.country=C.country and B.sex=C.sex
order by A.country,B.sexdrop table t1--------------------------------------------------------------------------------
村1 男 1
村1 女 0
村1 其他 0
村2 男 1
村2 女 1
村2 其他 0
村3 男 1
村3 女 1
村3 其他 1
create table t
(
country varchar(10),
peoplename varchar(10),
sex varchar(10)
)insert into t select '村1', '某甲', '男'
union all select '村2', '某乙', '女'
union all select '村3', '某丙', '男'
union all select '村2', '张三', '男'
union all select '村3', '李四', '其他'
union all select '村3', '王五', '女'select m.* ,isnull (n.c,0)
from
(select *
from
(
select distinct country from t
)a ,
(
select distinct sex from t
) b
) m left join
(select country ,sex ,count(1) as c from t group by country ,sex) n
on m.country =n.country and m.sex =n.sex
order by m.countrycountry sex
---------- ---------- -----------
村1 男 1
村1 女 0
村1 其他 0
村2 男 1
村2 女 1
村2 其他 0
村3 男 1
村3 女 1
村3 其他 1(所影响的行数为 9 行)
----------------------------------------
SQL SERVER2000 查询分析器通过的。为什么不行啊?
(select distinct sex from b ) AS BB
order by 1,2
我group的字段是三个。
order by country经过测试,没有把为0的显示出来country peoplename sex
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
cun1 jack m
cun1 rose f
cun2 jim m
cun2 ducan m
cun2 tom o
cun3 ben m
cun3 lily f
cun3 joe f
cun3 roger m
cun4 john o (10 row(s) affected)country sex sexcount
-------------------------------------------------- -------------------------------------------------- -----------
cun1 f 1
cun1 m 1
cun2 m 2
cun2 o 1
cun3 f 2
cun3 m 2
cun4 o 1(7 row(s) affected)
insert @ta
select '村1', '某甲' , '男' union all
select '村2', '某乙' , '女' union all
select '村3', '某丙' , '男' union all
select '村2', '张三' , '男' union all
select '村3', '李四' , '其他' union all
select '村3', '王五' , '女'
select a.country,b.sex,counts=sum(case when c.country is null then 0 else 1 end)
from
(select distinct country from @ta) a
inner join
(select distinct sex from @ta) b on 1=1--这里的1=1主要使表达式正确
left join @ta c on a.country=c.country and b.sex=c.sex
group by a.country,b.sex order by a.country(所影响的行数为 6 行)country sex counts
------- ---------- -----------
村1 男 1
村1 女 0
村1 其他 0
村2 男 1
村2 女 1
村2 其他 0
村3 男 1
村3 女 1
村3 其他 1(所影响的行数为 9 行)
多谢各位的热心帮助。既然性能没问题,我现在最后一个问题:
我想把sex这个字段不从数据库里提重复的数据,而是自己设置好,男,女,其他。是怎么写。
我是怕所有的数据中都没有其他,这个时候消除重复提取出来的性别只有男和女的了,而我的程序是按照3个写的,就会报错。谢谢。而且我还有别的一个字段要分组,这个字段两个值是0和1。只要实现一个,其他我就会写了。再谢。
--改一下:
select m.* ,isnull (n.c,0)
from
(select *
from
(
select distinct country from t
)a ,
(
select '男' as sex union all select '女' union all select '其他'
) b
) m left join
(select country ,sex ,count(1) as c from t group by country ,sex) n
on m.country =n.country and m.sex =n.sex
order by m.country
select a.country,b.sex,counts=sum(case when c.country is null then 0 else 1 end)
from
(select distinct country from [table]) a
join (select sex='男' union select '女' union select '其它') b on 1=1
left join table c on a.country=c.country and b.sex=c.sex
group by a.country,b.sex