Access下
表结构如下:
单位 姓名 性别 民族
1     a    男 汉
2     b    男 汉  
2     c    女 满 
1     d    女 汉  
如何完才能做出类似这样的统计表:
单位   男   女   汉    满      小计
1      1    1     2             2
2      1    1     1     1       2
合计   2    2     3     1

解决方案 »

  1.   

    打击你一下,用一条SQL是不能实现的!
      

  2.   

    我也正在做这样的,用一条语句根本就统计不来
    sixgj(轰炸机) 说的对
      

  3.   

    我做了一下,不知道是否符合要求:
    首先,楼主给的表结构好象都是字符型的字段,按照我的想法,必须要增加一个字段
    “数量”,整型,值全部为1,SQL 语句如下:select 单位,sum(a) as 男,sum(b) as 女,sum(c) as 汉,sum(d) as 满 from 
    (select 单位,sum(数量) as a,0 as b,0 as c,0 as d from test2 where 性别='男'
    group by 单位
    union
    select 单位,0 as a,sum(数量) as b,0 as c,0 as d from test2 where 性别='女'
    group by 单位
    union
    select 单位,0 as a,0 as b,sum(数量) as c,0 as d from test2 where 民族='汉'
    group by 单位
    union 
    select 单位,0 as a,0 as b,0 as c,sum(数量) as d from test2 where 民族='满'
    group by 单位) aa
    group by 单位合计我不会写。如果合计是放在 DBGrid 外面的话是可以实现的。
      

  4.   

    楼主没有说要求一个语句实现吧?
    select 单位,count(性别),count(民族),count(单位) from table group by 单位
    这样可以得出:
    1      1    1     2             2
    2      1    1     1     1       2
    select count(单位),count(性别),count(民族) from table
    可得出:
    合计   2    2     3     1
      

  5.   


    select  a.单位,count(case 性别 when '男' then '' end) as '男',
                count(case 性别 when '女' then '' end) as '女',
                 count(case 民族 when '汉' then '' end) as '汉',
                count(case 民族 when '满' then '' end) as '满',
                count(*) as '小计'           
    from a1 a
    group by a.单位 union allselect '合计', count(case 性别 when '男' then '' end) as '男',
                count(case 性别 when '女' then '' end) as '女',
                 count(case 民族 when '汉' then '' end) as '汉',
                count(case 民族 when '满' then '' end) as '满',
                count(*) as '小计'           
    from a1 a
      

  6.   

    在EHLIB中GRID能实现这样的功能.