|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你看出了作者的深度?深处半米!当初是冲那么多的大牛给他写序才买的,后来才发现无啥内容,作者也只是才用几年的新手,百花了几十两银子,再次感叹当今社会的虚伪与浮躁
/*--原帖地点:http://community.csdn.net/Expert/topic/3845/3845290.xml?temp=.3689386--*/
--测试数据createtabletb(编号int,性子varchar(10),数目int,目标1decimal(10,1),目标2decimal)inserttbselect1,00,10,1.1,10unionallselect2,01,20,1.2,20unionallselect3,00,30,1.5,10unionallselect4,01,40,1.9,35unionallselect5,00,40,1.2,20
/*--处置请求
请求失掉下述了局:
a局限性子(00)性子(01)-------------------------------------------------------------目标1<1.0.00.001.0-1.29.63.631.3-1.59.38.381.9-1.99.00.00>=2.00.00目标1均匀值1.271.55目标2<10.00.0010-311.001.0031-50.00.00>=50.00.00目标2均匀值13.3327.50数目算计:80.0060.00------------------------------------------------------------------
分类申明:
局限性子(00)性子(01)目标1<1.0001.0-1.29(10+40)/(10+30+40)20/(20+40)1.3-1.5930/(10+30+40)01.6-1.99040/(20+40)>=200目标1均匀值:(1.1+1.5+1.2)/3(1.2+1.9)/2
目标2<100010-30(10+30+40)/(10+30+40)20/(20+40)31-50040/(20+40)>=5000目标2均匀值:(10+10+20)/3(20+35)/2
数目算计:10+30+4020+40--*/go
--查询处置selecta,局限,[性子(00)],[性子(01)]from(selecta=casea.idwhen1then目标1when21then目标2elseend,局限=a.lb,[性子(00)]=cast(casewhenb.a>0thenisnull(a.a*1./b.a,0)else0endasdecimal(10,2)),[性子(01)]=cast(casewhenb.a>0thenisnull(a.a*1./b.a,0)else0endasdecimal(10,2)),a.idfrom(selectb.id,b.lb,a=sum(casea.性子when00thena.数目end),b=sum(casea.性子when01thena.数目end)fromtbarightjoin(selectid=1,lb=<1.0,a=null,b=1.0unionallselectid=2,lb=1.0-1.29,a=1.0,b=1.3unionallselectid=3,lb=1.3-1.59,a=1.3,b=1.9unionallselectid=4,lb=1.9-1.99,a=1.9,b=2.0unionallselectid=5,lb=>=2,a=2.0,b=null)bona.目标1>=isnull(b.a,a.目标1)anda.目标1<isnull(b.b,a.目标1-1)groupbyb.id,b.lbunionallselectb.id,b.lb,a=sum(casea.性子when00thena.数目end),b=sum(casea.性子when01thena.数目end)fromtbarightjoin(selectid=21,lb=<10,a=null,b=10unionallselectid=22,lb=10-31,a=10,b=31unionallselectid=23,lb=31-50,a=31,b=51unionallselectid=25,lb=>=50,a=50,b=null)bona.目标2>=isnull(b.a,a.目标2)anda.目标2<isnull(b.b,a.目标2-1)groupbyb.id,b.lb)a,(selecta=isnull(sum(case性子when00then数目end),0),b=isnull(sum(case性子when01then数目end),0)fromtb)bunionallselect目标1均匀值,,cast(isnull(casewhencount(case性子when00then性子end)>0thensum(case性子when00then目标1end)*1./count(case性子when00then性子end)else0end,0)asdecimal(10,2)),cast(isnull(casewhencount(case性子when01then性子end)>0thensum(case性子when01then目标1end)*1./count(case性子when01then性子end)else0end,0)asdecimal(10,2)),id=6fromtbunionallselect目标2均匀值,,cast(isnull(casewhencount(case性子when00then性子end)>0thensum(case性子when00then目标2end)*1./count(case性子when00then性子end)else0end,0)asdecimal(10,2)),cast(isnull(casewhencount(case性子when01then性子end)>0thensum(case性子when01then目标2end)*1./count(case性子when01then性子end)else0end,0)asdecimal(10,2)),id=26fromtbunionallselect数目算计:,,isnull(sum(case性子when00then数目end),0),isnull(sum(case性子when01then数目end),0),id=30fromtb)aorderbyidgo
--删除测试droptabletb
对于update操作,只需要把event中的旧行和新行值对调即可。 |
|