/*--原帖地址:
http://community.csdn.net/Expert/topic/3845/3845290.xml?temp=.3689386
--*/
--测试数据
create table tb(编号 int,性质 varchar(10),数量 int,指标1 decimal(10,1),指标2 decimal)
insert tb select 1 ,'00' ,10,1.1 ,10
union all select 2 ,'01' ,20,1.2 ,20
union all select 3 ,'00' ,30,1.5 ,10
union all select 4 ,'01' ,40,1.9 ,35
union all select 5 ,'00' ,40,1.2 ,20
/*--处理要求
要求得到下述结果:
a 范围 性质(00) 性质(01)
----------------- ---------------- -------------- --------------
指标1 <1.0 .00 .00
1.0-1.29 .63 .63
1.3-1.59 .38 .38
1.9-1.99 .00 .00
>=2 .00 .00
指标1平均值 1.27 1.55
指标2 <10 .00 .00
10-31 1.00 1.00
31-50 .00 .00
>=50 .00 .00
指标2平均值 13.33 27.50
数量合计: 80.00 60.00
------------------------------------------------------------------
分类说明:
范围 性质(00) 性质(01)
指标1 <1.0 0 0
1.0-1.29 (10+40)/(10+30+40) 20/(20+40)
1.3-1.59 30/(10+30+40) 0
1.6-1.99 0 40/(20+40)
>=2 0 0
指标1平均值: (1.1+1.5+1.2)/3 (1.2+1.9)/2
指标2 <10 0 0
10-30 (10+30+40)/(10+30+40) 20/(20+40)
31-50 0 40/(20+40)
>=500 0
指标2平均值: (10+10+20)/3 (20+35)/2
数量合计: 10+30+40 20+40
--*/
go
--查询处理
select a,范围,[性质(00)],[性质(01)]
from(
select
a=case a.id when 1 then '指标1' when 21 then '指标2' else '' end,
范围=a.lb,
[性质(00)]=cast(case when b.a>0 then isnull(a.a*1./b.a,0) else 0 end as decimal(10,2)),
[性质(01)]=cast(case when b.a>0 then isnull(a.a*1./b.a,0) else 0 end as decimal(10,2)),
a.id
from(
select b.id,b.lb,
a=sum(case a.性质 when '00' then a.数量 end),
b=sum(case a.性质 when '01' then a.数量 end)
from tb a
right join(
select id=1,lb='<1.0' ,a=null,b=1.0 union all
select id=2,lb='1.0-1.29',a=1.0 ,b=1.3 union all
select id=3,lb='1.3-1.59',a=1.3 ,b=1.9 union all
select id=4,lb='1.9-1.99',a=1.9 ,b=2.0 union all
select id=5,lb='>=2' ,a=2.0 ,b=null
)b on a.指标1>=isnull(b.a,a.指标1)
and a.指标1<isnull(b.b,a.指标1-1)
group by b.id,b.lb
union all
select b.id,b.lb,
a=sum(case a.性质 when '00' then a.数量 end),
b=sum(case a.性质 when '01' then a.数量 end)
from tb a right join(
select id=21,lb='<10' ,a=null,b=10 union all
select id=22,lb='10-31',a=10 ,b=31 union all
select id=23,lb='31-50',a=31 ,b=51 union all
select id=25,lb='>=50' ,a=50 ,b=null
)b on a.指标2>=isnull(b.a,a.指标2)
and a.指标2<isnull(b.b,a.指标2-1)
group by b.id,b.lb
)a,(
select
a=isnull(sum(case 性质 when '00' then 数量 end),0),
b=isnull(sum(case 性质 when '01' then 数量 end),0)
from tb
)b
union all
select '指标1平均值','',
cast(isnull(
case
when count(case 性质 when '00' then 性质 end)>0
then sum(case 性质 when '00' then 指标1 end)
*1./count(case 性质 when '00' then 性质 end)
else 0
end,0) as decimal(10,2)),
cast(isnull(
case
when count(case 性质 when '01' then 性质 end)>0
then sum(case 性质 when '01' then 指标1 end)
*1./count(case 性质 when '01' then 性质 end)
else 0
end,0) as decimal(10,2)),
id=6
from tb
union all
select '指标2平均值','',
cast(isnull(
case
when count(case 性质 when '00' then 性质 end)>0
then sum(case 性质 when '00' then 指标2 end)
*1./count(case 性质 when '00' then 性质 end)
else 0
end,0) as decimal(10,2)),
cast(isnull(
case
when count(case 性质 when '01' then 性质 end)>0
then sum(case 性质 when '01' then 指标2 end)
*1./count(case 性质 when '01' then 性质 end)
else 0
end,0) as decimal(10,2)),
id=26
from tb
union all
select '数量合计:','',
isnull(sum(case 性质 when '00' then 数量 end),0),
isnull(sum(case 性质 when '01' then 数量 end),0),
id=30
from tb
)a order by id
go
--删除测试
drop table tb
分享到:
相关推荐
高中生物遗传规律知识点全汇总+方法综述.doc
高中生物 遗传规律知识点全汇总+方法综述.doc
采用数值模拟的方法,分析了某矿4102回风平巷掘进过程中的应力分布和位移场分布规律,得出巷道掘进端头受到的强动压影响范围为距4101工作面-30~+80 m。采用分段控制技术,对巷道强动压影响段采取高强大延伸率锚杆和锚索...
2014高考地理轻取高分秘籍:突破地理思维模式之利用地理规律(规律总结+示例分析+提升演练,26页,含答案).doc
讨论了影响曲线形态(方向性和弯曲性)的特征参量,在对特征参量进行主成分分析的基础上提出了一种基于贝叶斯方法进行曲线分类而最终实现海岸线分段的模型。其基本步骤是将一定大小的窗口以一定的步长在待分段的海岸...
高中生物遗传规律知识点全归纳+方法综述.doc
平面直角坐标系找规律题型分类汇总解析.doc
图形推理规律汇总+典型题汇总。图形推理的两大灵魂是数量关系和图形的转动。牢牢把握住这两大灵魂就基本把握了图形推理题目。在这两大灵魂统帅下的十大基本规律,是每个想要在公考中取得优异成绩的考生必须系统熟练...
课标版2021高考物理一轮复习第一章直线运动第2讲匀变速直线运动的规律及应用教材研读+夯基提能作业含解析
分段压裂水平井产能分析方法研究,赵志成,廖锐全,应用格林函数法,建立了分段压裂水平井产能模型。根据此模型可得到水驱条件下分段压裂水平井各条裂缝产量随时间变化规律。计算结
为了揭示急倾斜特厚煤层水平分段综放开采条件下的覆岩活动规律,采用相似材料模拟试验,研究了不同水平分段开采后急倾斜煤层覆岩破坏过程、应力场变化和顶板沉陷规律。研究表明:覆岩的破裂经历裂隙产生、顶板离层、...
根据急倾斜煤层综采走向分段充填开采采空区下部自然冒落矸石和充填柱体布置特点,利用弹性薄板理论建立了急倾斜综采走向分段充填的力学模型,推导了顶板在上覆岩层、自然冒落矸石和采空区分段充填共同作用下的变形挠曲...
利用FLAC3D软件分析了水平分段开采工作面围岩应力及裂隙发育规律,旨在为后期瓦斯治理提供理论基础。研究表明:在煤层顶板出现明显卸压,卸压区类似"O"形分布,但倾向应力并不对称,呈现上部卸压,下部应力集中;开采后,...
针对数控机床参考点无规律漂移的现象,基于参考点返回操作的机理分析,借助诊断号302与STATUS状态子画面,先用栅格...同时给出了参考点无规律漂移的其他9种原因及处理方法。本文对机床制造与用户具有相当高的参考价值。
基于近直立特厚煤层水平分段开采过程中发生的强矿压显现问题,笔者通过现场实测、力学分析与数值模拟计算的方法研究了其围岩运动特征及矿压显现应力演化规律。研究表明:近直立特厚煤层水平分段开采围岩运动受采掘扰动...
自定义随机数,再添加数据是往往想要自己定义一个id好而且还让她没有规律那么这时候就需要随机数了这个例子包括了所有Random 的例子!
基于急倾斜煤层水平分段综放开采时,顶板垮落所产生的冲击动量对工作面造成冲击,采用立体相似模拟实验建立相似模型,通过理论分析计算冲击动量,运用3DEC数值模拟对人工爆破强制放顶进行模拟分析。结果表明:在开采初期,...
一年级找规律练习题集汇总.pdf
为了研究急倾斜煤层综采走向分段胶结充填覆层变形破断特性及移动规律,建立了倾向岩梁的力学模型,推导了倾向方向上覆岩梁的挠曲方程,利用方程求出了充填区域和未充填区域的最大挠度位置。并以湘永煤矿2463工作面为...