以下是我的存储过程<br><br><br>CREATE procedure yxh <br><br>as <br><br>begin <br>set nocount on<br><br>--------前两个月的成交量有效户<br>select left(ls.bizdate,6) ym,ls.fundid,ls.moneytype,sum(ls.matchamt) cjl,sum(ls.fee_jsxf) jsxf<br> into #Bcjlyxh <br> from his..h_logasset ls ,run..brokercust br ,run..custbaseinfo base <br> where ls.fundid=br.fundid <br> and ls.fundid=base.custid <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> and (ls.bizdate>=20080201 and bizdate<=20080331)<br> and (base.opendate>=20080201 and base.opendate<=20080331)<br> and ls.fee_jsxf>0 <br> group by left(ls.bizdate,6),ls.fundid,ls.moneytype <br> having (sum(ls.matchamt)>=10000 and moneytype=0) <br> or (sum(ls.matchamt)>=10000/0.9 and moneytype=1) <br> or (sum(ls.matchamt)>=10000/7.0 and moneytype=2)<br><br><br>--------前两个月的市值有效户<br>select zj.fundid,zj.moneytype,max(fundbal+marketvalue) gsz,max(marketvalue) valsz into #szyxh <br> from run..hiszjk zj,run..brokercust br <br> where zj.fundid=br.fundid <br> and (rq>=20080201 and rq<=20080331) <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> group by zj.fundid,zj.moneytype <br> having (max(marketvalue)>=20000 and moneytype=0) <br> or (max(marketvalue)>=20000/0.9 and moneytype=1) <br> or (max(marketvalue)>=20000/7.0 and moneytype=2)<br><br><br>------ 以下为前两个月已转化的全部有效户详细信息<br>select moneytype,fundid into #allB1B2m from #Bcjlyxh <br> union <br> select moneytype,fundid jsxf from #szyxh <br><br>select t.*,fundname,opendate into #allB1B2m1 <br> from #allB1B2m t,run..fundinfo base <br> where t.fundid=base.fundid <br> <br><br>select t.*,base.fullname khjlname,br.brokerid custid into #allB1B2m2 <br> from #allB1B2m1 t,run..custbaseinfo base,run..brokercust br <br> where t.fundid=br.fundid and br.brokerid=base.custid <br><br>select moneytype,fundid,fundname fullname,opendate,0 gsz,0 valsz,0 cjl,0 jsxf,khjlname,custid,1 Valid <br> into #allB1B2m3 <br> from #allB1B2m2 where opendate>=20080201<br> <br>----- 以上为前两个月已转化的全部有效户详细信息<br><br>select distinct fundid into #allB1B2 from #allB1B2m <br><br>------当月有效户计算<br>select ls.fundid,ls.moneytype,base.fullname,base.opendate,sum(ls.matchamt) cjl,sum(ls.fee_jsxf) jsxf <br> into #ttt <br> from his..h_logasset ls,run..brokercust br ,run..custbaseinfo base <br> where ls.fundid=br.fundid <br> and ls.fundid=base.custid <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> and (ls.bizdate>=20080401 and ls.bizdate<=20080430)<br> and (base.opendate>=20080201 and base.opendate<=20080430)<br> and ls.fee_jsxf>0 <br> group by ls.fundid,ls.moneytype,base.fullname,base.opendate<br> having (sum(ls.matchamt)>=10000 and moneytype=0) <br> or (sum(ls.matchamt)>=10000/0.9 and moneytype=1) <br> or (sum(ls.matchamt)>=10000/7.0 and moneytype=2)<br><br>select #ttt.*,base.fullname khjlname,base.custid into #cjlt <br> from #ttt,run..brokercust br,run..custbaseinfo base <br> where #ttt.fundid=br.fundid and br.brokerid=base.custid <br><br>select zj.fundid,zj.moneytype,max(fundbal+marketvalue) gsz,max(marketvalue) valsz into #tvalt3 <br> from run..hiszjk zj,run..brokercust br <br> where zj.fundid=br.fundid <br> and (rq>=20080401 and rq<=20080430) <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> group by zj.fundid,zj.moneytype<br> having (max(marketvalue)>=20000 and moneytype=0) <br> or (max(marketvalue)>=20000/0.9 and moneytype=1) <br> or (max(marketvalue)>=20000/7.0 and moneytype=2)<br><br>select #tvalt3.*,base.fullname khjlname,base.custid into #tvalt4 <br> from #tvalt3,run..brokercust br,run..custbaseinfo base <br> where #tvalt3.fundid=br.fundid and br.brokerid=base.custid <br><br>select #tvalt4.* into #tvalt5 <br> from #tvalt4,run..custbaseinfo base <br> where #tvalt4.fundid=base.custid<br> and (base.opendate>=20080201 and base.opendate<=20080430)<br><br>select #tvalt5.moneytype,#tvalt5.fundid,base.fullname,base.opendate,#tvalt5.gsz,#tvalt5.valsz,#tvalt5.khjlname,<br> #tvalt5.custid<br> into #valt <br> from #tvalt5,run..custbaseinfo base <br> where #tvalt5.fundid=base.custid <br><br>select moneytype,fundid,fullname,opendate,gsz,valsz,0 cjl,0 jsxf,khjlname,custid into #tallt <br> from #valt<br> union all <br> select moneytype,fundid,fullname,opendate,0 gsz,0 valsz,cjl,jsxf,khjlname,custid from #cjlt <br><br><br>select moneytype,fundid,fullname,opendate,sum(gsz) gsz,sum(valsz) valsz,sum(cjl) cjl,sum(jsxf) jsxf,<br> khjlname,custid<br> into #allt<br> from #tallt<br> group by moneytype,fundid,fullname,opendate,khjlname,custid <br><br>select *,2 Valid into #yallrt<br> from #allt <br> where fundid not in (select fundid from #allB1B2)<br><br>elect #yallrt.*, mem.groupid gid,mem.groupname name into #y1 <br> from #yallrt<br> left join xm..brokergroupmem mem <br> on #yallrt.custid=mem.brokerid <br><br>select identity(int,1,1) as odr,moneytype,fundid,fullname,opendate,<br> gsz,valsz,cjl,jsxf,khjlname,custid,gid,name,Valid into #yaaw<br> from #y1 <br> order by custid asc,Valid desc,fundid asc<br><br>select * from #yaaw<br><br>end<br><br><br>如果直接运行 exec yxh 则时间很长<br><br>但是如果不用存储过程,这样:<br><br> <br>set nocount on<br><br>--------前两个月的成交量有效户<br>select left(ls.bizdate,6) ym,ls.fundid,ls.moneytype,sum(ls.matchamt) cjl,sum(ls.fee_jsxf) jsxf<br> into #Bcjlyxh <br> from his..h_logasset ls ,run..brokercust br ,run..custbaseinfo base <br> where ls.fundid=br.fundid <br> and ls.fundid=base.custid <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> and (ls.bizdate>=20080201 and bizdate<=20080331)<br> and (base.opendate>=20080201 and base.opendate<=20080331)<br> and ls.fee_jsxf>0 <br> group by left(ls.bizdate,6),ls.fundid,ls.moneytype <br> having (sum(ls.matchamt)>=10000 and moneytype=0) <br> or (sum(ls.matchamt)>=10000/0.9 and moneytype=1) <br> or (sum(ls.matchamt)>=10000/7.0 and moneytype=2)<br>go<br><br>--------前两个月的市值有效户<br>select zj.fundid,zj.moneytype,max(fundbal+marketvalue) gsz,max(marketvalue) valsz into #szyxh <br> from run..hiszjk zj,run..brokercust br <br> where zj.fundid=br.fundid <br> and (rq>=20080201 and rq<=20080331) <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> group by zj.fundid,zj.moneytype <br> having (max(marketvalue)>=20000 and moneytype=0) <br> or (max(marketvalue)>=20000/0.9 and moneytype=1) <br> or (max(marketvalue)>=20000/7.0 and moneytype=2)<br>go<br><br>------ 以下为前两个月已转化的全部有效户详细信息<br>select moneytype,fundid into #allB1B2m from #Bcjlyxh <br> union <br> select moneytype,fundid jsxf from #szyxh <br>go<br><br>select t.*,fundname,opendate into #allB1B2m1 <br> from #allB1B2m t,run..fundinfo base <br> where t.fundid=base.fundid <br>go <br><br>select t.*,base.fullname khjlname,br.brokerid custid into #allB1B2m2 <br> from #allB1B2m1 t,run..custbaseinfo base,run..brokercust br <br> where t.fundid=br.fundid and br.brokerid=base.custid <br>go<br><br>select moneytype,fundid,fundname fullname,opendate,0 gsz,0 valsz,0 cjl,0 jsxf,khjlname,custid,1 Valid <br> into #allB1B2m3 <br> from #allB1B2m2 where opendate>=20080201<br>go<br> <br>----- 以上为前两个月已转化的全部有效户详细信息<br><br>select distinct fundid into #allB1B2 from #allB1B2m <br>go<br><br>------当月有效户计算<br>select ls.fundid,ls.moneytype,base.fullname,base.opendate,sum(ls.matchamt) cjl,sum(ls.fee_jsxf) jsxf <br> into #ttt <br> from his..h_logasset ls,run..brokercust br ,run..custbaseinfo base <br> where ls.fundid=br.fundid <br> and ls.fundid=base.custid <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> and (ls.bizdate>=20080401 and ls.bizdate<=20080430)<br> and (base.opendate>=20080201 and base.opendate<=20080430)<br> and ls.fee_jsxf>0 <br> group by ls.fundid,ls.moneytype,base.fullname,base.opendate<br> having (sum(ls.matchamt)>=10000 and moneytype=0) <br> or (sum(ls.matchamt)>=10000/0.9 and moneytype=1) <br> or (sum(ls.matchamt)>=10000/7.0 and moneytype=2)<br>go<br><br>select #ttt.*,base.fullname khjlname,base.custid into #cjlt <br> from #ttt,run..brokercust br,run..custbaseinfo base <br> where #ttt.fundid=br.fundid and br.brokerid=base.custid <br>go<br><br>select zj.fundid,zj.moneytype,max(fundbal+marketvalue) gsz,max(marketvalue) valsz into #tvalt3 <br> from run..hiszjk zj,run..brokercust br <br> where zj.fundid=br.fundid <br> and (rq>=20080401 and rq<=20080430) <br> and substring(cast(br.brokerid as char(12)),5,2)='92'<br> group by zj.fundid,zj.moneytype<br> having (max(marketvalue)>=20000 and moneytype=0) <br> or (max(marketvalue)>=20000/0.9 and moneytype=1) <br> or (max(marketvalue)>=20000/7.0 and moneytype=2)<br>go<br><br>select #tvalt3.*,base.fullname khjlname,base.custid into #tvalt4 <br> from #tvalt3,run..brokercust br,run..custbaseinfo base <br> where #tvalt3.fundid=br.fundid and br.brokerid=base.custid <br>go<br><br>select #tvalt4.* into #tvalt5 <br> from #tvalt4,run..custbaseinfo base <br> where #tvalt4.fundid=base.custid<br> and (base.opendate>=20080201 and base.opendate<=20080430)<br>go<br><br>select #tvalt5.moneytype,#tvalt5.fundid,base.fullname,base.opendate,#tvalt5.gsz,#tvalt5.valsz,#tvalt5.khjlname,<br> #tvalt5.custid<br> into #valt <br> from #tvalt5,run..custbaseinfo base <br> where #tvalt5.fundid=base.custid <br>go<br><br>select moneytype,fundid,fullname,opendate,gsz,valsz,0 cjl,0 jsxf,khjlname,custid into #tallt <br> from #valt<br> union all <br> select moneytype,fundid,fullname,opendate,0 gsz,0 valsz,cjl,jsxf,khjlname,custid from #cjlt <br>go<br><br>select moneytype,fundid,fullname,opendate,sum(gsz) gsz,sum(valsz) valsz,sum(cjl) cjl,sum(jsxf) jsxf,<br> khjlname,custid<br> into #allt<br> from #tallt<br> group by moneytype,fundid,fullname,opendate,khjlname,custid <br>go<br><br>select *,2 Valid into #yallrt<br> from #allt <br> where fundid not in (select fundid from #allB1B2)<br>go<br><br>elect #yallrt.*, mem.groupid gid,mem.groupname name into #y1 <br> from #yallrt<br> left join xm..brokergroupmem mem <br> on #yallrt.custid=mem.brokerid <br>go<br><br>select identity(int,1,1) as odr,moneytype,fundid,fullname,opendate,<br> gsz,valsz,cjl,jsxf,khjlname,custid,gid,name,Valid into #yaaw<br> from #y1 <br> order by custid asc,Valid desc,fundid asc<br>go<br><br>select * from #yaaw<br>go<br><br>就会快很多,我算了一下,会快10倍,不知道什么原因