假设一个表
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谢谢。

解决方案 »

  1.   

    select case when count(1)>=1 then 1 else 0 end from table group by country,sex
      

  2.   

    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
      

  3.   

    create table t1
    (
    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
      

  4.   

    to小狂花,不行哦to 懒猫,我没试验过,但是效率会不会太低。因为表中是几十万甚至上百万的数据进行汇总。
      

  5.   


    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 行)
      

  6.   

    to小狂花,不行哦
    ----------------------------------------
    SQL SERVER2000 查询分析器通过的。为什么不行啊?
      

  7.   

    其中表用b代替select country,sex,(select count(*) from b where country = AA.country and sex = bb.sex) as count from(select distinct country from b) as AA,
    (select distinct sex from b ) AS BB
    order by 1,2
      

  8.   

    to 小狂花。可以实现,但是效率会不会太低。
    我group的字段是三个。
      

  9.   

    select country,sex,count(sex) as sexcount from Table_country group by country,sex
    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)
      

  10.   

    declare @ta table(country varchar(5), peoplename varchar(5), sex varchar(10))
    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 行)
      

  11.   

    to smaworm:呵呵,没办法,客户要求。
    多谢各位的热心帮助。既然性能没问题,我现在最后一个问题:
    我想把sex这个字段不从数据库里提重复的数据,而是自己设置好,男,女,其他。是怎么写。
    我是怕所有的数据中都没有其他,这个时候消除重复提取出来的性别只有男和女的了,而我的程序是按照3个写的,就会报错。谢谢。而且我还有别的一个字段要分组,这个字段两个值是0和1。只要实现一个,其他我就会写了。再谢。
      

  12.   

    我是怕所有的数据中都没有其他,这个时候消除重复提取出来的性别只有男和女的了,而我的程序是按照3个写的,就会报错。谢谢。----------------
    --改一下:
    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
      

  13.   

    非常感谢。呵呵,我怎么把union给忘了。结贴,分不多。呵呵。
      

  14.   

    有懒猫的比较好! 
    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
      

  15.   

    我的sql你估计都没试过吧。我可是试过了的。