我做了一下,不知道是否符合要求: 首先,楼主给的表结构好象都是字符型的字段,按照我的想法,必须要增加一个字段 “数量”,整型,值全部为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 外面的话是可以实现的。
楼主没有说要求一个语句实现吧? 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
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
sixgj(轰炸机) 说的对
首先,楼主给的表结构好象都是字符型的字段,按照我的想法,必须要增加一个字段
“数量”,整型,值全部为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 外面的话是可以实现的。
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
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