比较难的SQL语句,看来只能用存储过程了。(50分)

  • 主题发起人 主题发起人 zyt_1978
  • 开始时间 开始时间
Z

zyt_1978

Unregistered / Unconfirmed
GUEST, unregistred user!
业务员 展位费 &nbsp;会务费 &nbsp; &nbsp;会刊广告费 &nbsp;场地广告费<br>&nbsp;小王 &nbsp; &nbsp;1200 &nbsp; &nbsp; 800 &nbsp; &nbsp; &nbsp;300 &nbsp; &nbsp; &nbsp; &nbsp; 800<br>&nbsp;小李 &nbsp; 2600 &nbsp; &nbsp; 1200 &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp; &nbsp;1200<br><br>费用类型名称 &nbsp; &nbsp;起始金额 &nbsp; &nbsp;终止金额 &nbsp; 提成比例 &nbsp; <br>展位费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp; &nbsp; &nbsp; 1000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25 &nbsp;<br>展位费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1000 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30 &nbsp;<br>展位费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp;3000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35 &nbsp; <br>会务费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp; &nbsp; &nbsp; 1000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25 &nbsp;<br>会务费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1000 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30 &nbsp;<br>会务费 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp;3000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35 &nbsp; <br>会刊广告费 &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp; &nbsp; &nbsp; 1000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25 &nbsp;<br>会刊广告费 &nbsp; &nbsp; &nbsp; &nbsp;1000 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30 &nbsp;<br>会刊广告费 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp;3000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35<br>场地广告费 &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp; &nbsp; &nbsp; 1000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25 &nbsp;<br>场地广告费 &nbsp; &nbsp; &nbsp; &nbsp;1000 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30 &nbsp;<br>场地广告费 &nbsp; &nbsp; &nbsp; &nbsp;2000 &nbsp; &nbsp; &nbsp; &nbsp;3000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35<br><br><br>怎样得到业务员的提成
 
没有搞懂LZ要表达得问题。[?]
 
select [业务员], <br>&nbsp; [展位费提成] = [展位费] * t2.[提成比例] / 100,<br>&nbsp; [会务费提成] = [会务费] * t3.[提成比例] / 100,<br>&nbsp; [会刊广告费提成] = [会刊广告费] * t4.[提成比例] / 100,<br>&nbsp; [场地广告费提成] = [场地广告费] * t5.[提成比例] / 100,<br>&nbsp; [合计]=isnull([展位费] * t2.[提成比例] / 100,0) +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isnull([会务费] * t3.[提成比例] / 100,0) +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isnull([会刊广告费] * t4.[提成比例] / 100,0) + <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isnull([场地广告费] * t5.[提成比例] / 100,0)<br>from Table1 t1<br>left join Table2 t2 on(t2.[费用类型名称]='展位费' and t2.[起始金额]&lt;=t1.[展位费] and t2.[终止金额]&gt;=t1.[展位费]) &nbsp;<br>left join Table2 t3 on(t3.[费用类型名称]='会务费' and t3.[起始金额]&lt;=t1.[会务费] and t3.[终止金额]&gt;=t1.[会务费]) &nbsp;<br>left join Table2 t4 on(t4.[费用类型名称]='会刊广告费' and t4.[起始金额]&lt;=t1.[会刊广告费] and t4.[终止金额]&gt;=t1.[会刊广告费]) &nbsp;<br>left join Table2 t5 on(t5.[费用类型名称]='场地广告费' and t5.[起始金额]&lt;=t1.[场地广告费] and t5.[终止金额]&gt;=t1.[场地广告费])
 
create &nbsp; proc proc_ywytc<br><br>&nbsp; @proid varchar(50)<br>as<br><br>&nbsp; /*<br><br>&nbsp; exec proc_ywytc<br><br>&nbsp; create proc proc_ywytc<br>&nbsp; @proid varchar(50)<br>&nbsp; as<br><br>&nbsp; SELECT * from V_skhz_ywy &nbsp;where &nbsp;proid='20080100'<br>&nbsp; <br>&nbsp; SELECT * from ywytc<br><br>&nbsp; */<br>&nbsp; declare @ywy varchar(50)<br>&nbsp; declare @zwf varchar(50)<br>&nbsp; declare @hwf varchar(50)<br>&nbsp; declare @ggf varchar(50)<br>&nbsp; declare @cdggf varchar(50)<br>&nbsp; declare @xcggf varchar(50)<br>&nbsp; declare @qtfy varchar(50)<br><br>&nbsp; declare @qsje decimal(18,2)<br>&nbsp; declare @zzje decimal(18,2)<br><br>&nbsp; declare @tcbl decimal(18,2)<br>&nbsp; declare @tc decimal(18,2)<br>&nbsp; declare @tce decimal(18,2)<br>&nbsp; declare @tcs decimal(18,2)<br>&nbsp; declare @tcsi decimal(18,2)<br>&nbsp; declare @tcw decimal(18,2)<br>&nbsp; declare @tcl decimal(18,2)<br><br>&nbsp; declare @ibcount int<br>&nbsp; declare @iecount int<br><br>&nbsp; --创建临时表 &nbsp; 两个字段<br>&nbsp; create table #ywytc <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(业务人员 varchar(10),展位费 decimal(18,2),提成一 &nbsp;decimal(18,2),会务费 &nbsp;decimal(18,2),提成二 &nbsp;decimal(18,2), 会刊广告费 &nbsp;decimal(18,2),提成三 &nbsp;decimal(18,2), 场地广告费 &nbsp;decimal(18,2),提成四 &nbsp;decimal(18,2),宣传广告费 &nbsp;decimal(18,2),提成五 &nbsp;decimal(18,2),其他费用 &nbsp;decimal(18,2),提成六 &nbsp;decimal(18,2),总提成额 &nbsp;decimal(18,2))<br>&nbsp;<br>&nbsp; declare ff cursor for &nbsp;SELECT ywy,zwf,hwf,ggf,cdggf,xcggf,qtfy from V_skhz_ywy where &nbsp;proid=@proid<br><br>&nbsp; set @ibcount=0<br>&nbsp; select @iecount=count(*) from V_skhz_ywy where &nbsp;proid=@proid<br>&nbsp; open ff<br>&nbsp; while @ibcount&lt;=@iecount-1<br>&nbsp; begin<br>&nbsp; &nbsp; fetch next from ff<br>&nbsp; &nbsp; into @ywy,@zwf,@hwf,@ggf,@cdggf,@xcggf,@qtfy<br><br>&nbsp; &nbsp; /* &nbsp; &nbsp;<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='zwf'<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='hwf'<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='ggf'<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='cdggf'<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='xcggf'<br>&nbsp; &nbsp; SELECT * from ywytc where 费用类型编号='qtfy'<br>&nbsp; &nbsp; */<br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='zwf'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp; while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @zwf&gt;=@qsje and @zwf&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set @tc=@zwf*@tcbl/100 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='hwf'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp;while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @hwf&gt;=@qsje and @hwf&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set @tce=@hwf*@tcbl/100 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='ggf'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp;while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if @ggf&gt;=@qsje and @ggf&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set @tcs=@ggf*@tcbl/100 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='cdggf'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp; while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @cdggf&gt;=@qsje and @cdggf&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set @tcsi=@cdggf*@tcbl/100 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='xcggf'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp; while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @xcggf&gt;=@qsje and @xcggf&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set @tcw=@xcggf*@tcbl/100 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br>&nbsp; <br><br>&nbsp; &nbsp; &nbsp; declare tc cursor for SELECT 起始金额,终止金额,提成比例 from ywytc where 展会项目编号=@proid and 费用类型编号='qtfy'<br>&nbsp; &nbsp; &nbsp; open &nbsp;tc<br>&nbsp; &nbsp; &nbsp; fetch next from tc into @qsje,@zzje,@tcbl<br>&nbsp; &nbsp; &nbsp; &nbsp; while &nbsp;@@FETCH_STATUS = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @qtfy&gt;=@qsje and @qtfy&lt;=@zzje<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set @tcl=@qtfy*@tcbl/100 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fetch next from tc into @qsje,@zzje,@tcbl &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; close tc<br>&nbsp; &nbsp; &nbsp; deallocate tc<br><br><br>&nbsp; insert #ywytc <br>&nbsp; &nbsp; select @ywy,@zwf,@tc,@hwf,@tce,@ggf,@tcs,@cdggf,@tcsi,@xcggf,@tcw,@qtfy,@tcl,@tc+@tce+@tcs+@tcsi+@tcw+@tcl<br>&nbsp; &nbsp; --select @ywy 业务人员,@zwf 展位费,@tc 提成,@hwf 会务费,@tce 提成,@ggf 会刊广告费,@tcs 提成,@cdggf 场地广告费,@tcsi 提成,@xcggf 宣传广告费,@tcw 提成,@qtfy 其他费用,@tcl 提成,@tc+@tce+@tcs+@tcsi+@tcw+@tcl 总提成额<br><br>&nbsp; &nbsp; set @ibcount=@ibcount+1<br>&nbsp; end<br>&nbsp; close ff<br>&nbsp; deallocate ff<br><br>&nbsp; select * from #ywytc<br>&nbsp; <br>&nbsp; /*<br><br>&nbsp; DECLARE @RC int<br>DECLARE @proid varchar(50)<br>SELECT @proid = '20080100'<br>EXEC @RC = [dbo].[proc_ywytc] @proid<br>DECLARE @PrnLine nvarchar(4000)<br>PRINT '存储过程: dbo.proc_ywytc'<br>SELECT @PrnLine = ' 返回代码 = ' + CONVERT(nvarchar, @RC)<br>PRINT @PrnLine<br><br>&nbsp; &nbsp;*/<br><br>GO<br><br><br>@proid 是项目编号<br>我写了一个存储过程,完成了,大家看看还有没有更好的办法。
 
嗯,还是 kaida 厉害呀<br>原来技巧在 on 里面 我写的太笨了。<br><br>conlin同学还要努力呀,呵呵,人家kaida一看就很明白。
 
晕,之前没有反应过来,业务太不熟悉拉,^_^
 
后退
顶部