表
jylb yplb zs hgs hgl
监督检测 旅店业 1 1 1
监督检测 蒸馏酒 5 4 0.8
委托检测 旅店业 1 1 1
委托检测 蒸馏酒 2 2 1
仲裁检测 蒸馏酒 2 2 1 通过查询,要求用dbgrid显示如下
合计 旅店业 蒸馏酒
合计 监测件数 11 2 9
合格件数 10 2 8
合格率 0.9 1 0.8
监督检测 监测件数 6 1 5
合格件数 5 1 4
合格率 0.8 1 0.8
委托检测 监测件数 3 1 2
合格件数 3 1 2
合格率 1 1 1
仲裁检测 监测件数 2 / 2
合格件数 2 / 2
合格率 1 / 1
jylb yplb zs hgs hgl
监督检测 旅店业 1 1 1
监督检测 蒸馏酒 5 4 0.8
委托检测 旅店业 1 1 1
委托检测 蒸馏酒 2 2 1
仲裁检测 蒸馏酒 2 2 1 通过查询,要求用dbgrid显示如下
合计 旅店业 蒸馏酒
合计 监测件数 11 2 9
合格件数 10 2 8
合格率 0.9 1 0.8
监督检测 监测件数 6 1 5
合格件数 5 1 4
合格率 0.8 1 0.8
委托检测 监测件数 3 1 2
合格件数 3 1 2
合格率 1 1 1
仲裁检测 监测件数 2 / 2
合格件数 2 / 2
合格率 1 / 1
create table 表 (jylb varchar(8),yplb varchar(6),zs int,hgs int,hgl numeric(2,1))
insert into 表
select '监督检测','旅店业',1,1,1 union all
select '监督检测','蒸馏酒',5,4,0.8 union all
select '委托检测','旅店业',1,1,1 union all
select '委托检测','蒸馏酒',2,2,1 union all
select '仲裁检测','蒸馏酒',2,2,1
select [ ]=jylb,[ ]='监测件数',合计=ltrim(sum(zs)),
旅店业=ltrim(sum(case yplb when '旅店业' then zs else 0 end)),
蒸馏酒=ltrim(sum(case yplb when '蒸馏酒' then zs else 0 end)) from 表
group by jylb
union all
select [ ]=jylb,[ ]='合格件数',合计=ltrim(sum(hgs)),
旅店业=ltrim(sum(case yplb when '旅店业' then hgs else 0 end)),
蒸馏酒=ltrim(sum(case yplb when '蒸馏酒' then hgs else 0 end)) from 表
group by jylb
union all
select [ ]=jylb,[ ]='合格率',合计=left(sum(hgs)*1.0/sum(zs),3),
旅店业=case sum(case yplb when '旅店业' then zs else 0 end) when 0 then '/' else left(sum(case yplb when '旅店业' then hgs else 0 end)*1.0/sum(case yplb when '旅店业' then zs else 0 end),3) end,
蒸馏酒=case sum(case yplb when '蒸馏酒' then zs else 0 end) when 0 then '/' else left(sum(case yplb when '蒸馏酒' then hgs else 0 end)*1.0/sum(case yplb when '蒸馏酒' then zs else 0 end),3) end from 表
group by jylb
create table 表 (jylb varchar(8),yplb varchar(6),zs int,hgs int,hgl numeric(2,1))
insert into 表
select '监督检测','旅店业',1,1,1 union all
select '监督检测','蒸馏酒',5,4,0.8 union all
select '委托检测','旅店业',1,1,1 union all
select '委托检测','蒸馏酒',2,2,1 union all
select '仲裁检测','蒸馏酒',2,2,1select * from
(select [a]=isnull(jylb,'合计'),[b]='监测件数',合计=ltrim(sum(zs)),
旅店业=ltrim(sum(case yplb when '旅店业' then zs else 0 end)),
蒸馏酒=ltrim(sum(case yplb when '蒸馏酒' then zs else 0 end)) from 表
group by jylb with rollup
union all
select [a]=isnull(jylb,'合计'),[b]='合格件数',合计=ltrim(sum(hgs)),
旅店业=ltrim(sum(case yplb when '旅店业' then hgs else 0 end)),
蒸馏酒=ltrim(sum(case yplb when '蒸馏酒' then hgs else 0 end)) from 表
group by jylb with rollup
union all
select [a]=isnull(jylb,'合计'),[b]='合格率',合计=left(sum(hgs)*1.0/sum(zs),3),
旅店业=case sum(case yplb when '旅店业' then zs else 0 end) when 0 then '/' else left(sum(case yplb when '旅店业' then hgs else 0 end)*1.0/sum(case yplb when '旅店业' then zs else 0 end),3) end,
蒸馏酒=case sum(case yplb when '蒸馏酒' then zs else 0 end) when 0 then '/' else left(sum(case yplb when '蒸馏酒' then hgs else 0 end)*1.0/sum(case yplb when '蒸馏酒' then zs else 0 end),3) end from 表
group by jylb with rollup)a
order by a,case b when '监测件数' then 1 when '合格件数' then 2 else 3 end
--结果:
a b 合计 旅店业 蒸馏酒
-------- -------- ------------ ------------ ------------
合计 监测件数 11 2 9
合计 合格件数 10 2 8
合计 合格率 0.9 1.0 0.8
监督检测 监测件数 6 1 5
监督检测 合格件数 5 1 4
监督检测 合格率 0.8 1.0 0.8
委托检测 监测件数 3 1 2
委托检测 合格件数 3 1 2
委托检测 合格率 1.0 1.0 1.0
仲裁检测 监测件数 2 0 2
仲裁检测 合格件数 2 0 2
仲裁检测 合格率 1.0 / 1.0