代理商返佣问题,求递归查询 ####################百分比结贴

t101lian 2014-04-03 11:29:41
代理商表UserAuction
Uid ,parentId ,name ,PecenBail
78 ,0 ,华东代理 ,0.6
79 ,78 ,上海代理 ,0.5
80 ,79 ,上海浦东代理 ,0.2

用户表UserTb
Id , name , Uid
11111111 , 张三 ,79
22222222 , 李四 ,79
33333333 ,王五 ,80
44444444 ,刘六 ,78


合同表MatchedTb
Id ,CreateTime ,BUserId(为买家ID) ,SUserId(为卖家ID) Bail(为合同金额)
1 ,2014/3/19 14:48:35 ,22222222 , 11111111 50
2 ,2014/3/20 14:28:35 ,33333333 ,99999999 100

以上面数据为例
需求:得到如下结果

代理姓名 返佣金额
华东代理 60
上海代理 54
上海浦东代理 6
-------------------------------------------
返佣计算方法
------------------------------------------------------------
| 合同一 (ID为1的那一行) 合同二
| 交易金额 100 (买家和卖家都要计算) 100
|
代理姓名 返佣金额 |
华东代理 60 | 100*0.6-100*0.6*0.5 100*0.6-100*0.6*0.5
上海代理 54 | 100*0.6*0.5 100*0.6*0.5-100*0.6*0.5*0.2
上海浦东代理 6 | 0 100*0.6*0.5*0.2




-----------------------------
select  sum(b.Bail) from UserTb a inner join MatchedTb  b 
on a.Userid=b.BUserId or a.userId=b.SUserId
where a.Userid in('22222222','11111111') and b.CreateTime between '2014/3/18' and '2014/3/25'

这是自己测试时根据时间查询交易总金额的语句, 当然 a.Userid in('22222222','11111111') 肯定不行, 需要递归查询的。


。求大牛们写存储过程一个, 传入三个参数@Uid (代理商的ID) @startTime 和@endTime (合同表的时间)

...全文
283 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
t101lian 2014-04-14
  • 打赏
  • 举报
回复

ALTER proc  [dbo].[AgentQuery]
(
  @Uaid int ,@startTime datetime ,@EndTime datetime
)
 as 
    begin 
      if(isnull(@startTime,'')='')
        begin
            set @startTime='1900/01/01'
        end
      if(isnull(@EndTime,'')='')
         begin
            set @EndTime='2999/01/01' 
          end
 ;with cte  as
(
   select * ,leve=cast(1 as int),parent_pecent=cast(0 as float) from UserAuction a where not exists(select 1 from UserAuction where a.aid=uaid)
   union all 
   select a.* ,leve=cast (1+b.leve as int),parent_pecent=a.pecentBail*isnull(nullif(b.parent_pecent,0),b.pecentBail) from UserAuction a inner join cte b on a.aid=b.uaid
), bbb as
(
select  uaid,aid,AuUname,leve,pecentBail,parent_pecent,
 isnull((select sum(AgentSetBail) from UserTb a inner join MatchedTb  b 
on a.Userid=b.BUserId or a.userId=b.SUserId
 where a.AgentID =c.uaid  and b.CreateTime between @startTime and @EndTime),0) as ownBail
 from cte c  
)  
,ccc as
(
SELECT *,ISNULL((
case when exists (select 1 from bbb where aid=a.uaid) then 
(SELECT SUM(ownBail) AS Bail  FROM bbb b WHERE leve>=a.leve ) else 
(SELECT SUM(ownBail) AS Bail  FROM bbb b WHERE leve>a.leve )end
 ),ownBail)  Sumbail FROM bbb   a
) ,ddd as
(
select *,ReturnBail= isnull(nullif(parent_pecent,0),pecentBail)*Sumbail - isnull((select sum(parent_pecent*Sumbail) 
 from ccc where aid=a.uaid ),0)  from ccc a  
)
,fff as
(
    select * from ddd where Uaid=@uaid
    union all
    select a.* from ddd a inner join fff b on a.aid=b.uaid
)
select * from fff
  end

-----=========================================


ALTER proc  [dbo].[AgentMatchedTb]
(
   @Uaid int ,@UserName varchar(20),@startTime varchar(20) ,@EndTime varchar(20)
)
 as 
 begin 
  
if Object_id('tempdb..#AgentMatchedTb') is not null drop table #AgentMatchedTb
 declare @sql varchar(1000)
;with cte  as
(
   select * ,leve=cast(1 as int),parent_pecent=cast(0 as float) from UserAuction a where uaid=@Uaid
   union all 
   select a.* ,leve=cast (1+b.leve as int),parent_pecent=a.pecentBail*isnull(nullif(b.parent_pecent,0),b.pecentBail) from UserAuction a inner join cte b on a.aid=b.uaid
)
, bbb as
(
 select distinct   m.id,m.GoodsName, m.bail, m.price,m.buyMode,m.num,m.BUserId,m.SuserId,m.AgentSetBail,m.CreateTime from MatchedTb m 
  inner join (select UserId from  UserTb a inner join cte c on a.agentid=c.uaid ) u on u.UserId=m.BUserId or u.UserId=m.SuserId 
) 
  select * into #AgentMatchedTb from bbb
set @sql=' select * from #AgentMatchedTb  where 1=1 '
 
 if(isnull(@startTime,'')!='')
  begin 
  set @SQL=@SQL+' and  CreateTime >='''+@startTime+'''';
  end
  if(isnull(@EndTime,'')!='')
  begin
  set @SQL=@SQL+' and  CreateTime <='''+@EndTime+''''
  end
 if(isnull(@UserName,'')!='')
  begin 
 set @SQL=@SQL+' and SuserId ='''+ @UserName + ''' or BUserId='''+@UserName+''''
  end


 exec(@sql)
 drop table #AgentMatchedTb


end 

t101lian 2014-04-04
  • 打赏
  • 举报
回复
引用 10 楼 ap0405140 的回复:
[quote=引用 楼主 t101lian 的回复:] 返佣计算方法 ------------------------------------------------------------ | 合同一 (ID为1的那一行) 合同二 | 交易金额 100 (买家和卖家都要计算) 100 | 代理姓名 返佣金额 | 华东代理 60 | 100*0.6-100*0.6*0.5 100*0.6-100*0.6*0.5 上海代理 54 | 100*0.6*0.5 100*0.6*0.5-100*0.6*0.5*0.2 上海浦东代理 6 | 0 100*0.6*0.5*0.2
唐诗不才,这一段没有看到算法.请LZ讲解一下.. [/quote] 第二笔交易中 买家 33333333 所属的代理商是 上海浦东代理,卖家99999999 没有所属代理商,就不用管,所以就按交易金额 100 来算返佣。 这100元钱首先拿给上海浦东代理 最上面的 华东代理。华东的佣金比例是60%,所以他最先拿到的是60元,然后他又要从这60元中 拿出一部分给上海代理,上海代理的佣金比例上 50%,所以这样一来, 华东代理 60 | 100*0.6-100*0.6*0.5 100*0.6-100*0.6*0.5 就只有30 然后上海代理又接着分给浦东代理 上海代理 54 | 100*0.6*0.5 100*0.6*0.5-100*0.6*0.5*0.2 只有24 感谢版主,,,如果还有我没表达清楚的地方请指出。。
t101lian 2014-04-03
  • 打赏
  • 举报
回复
引用 3 楼 yupeigu 的回复:
[quote=引用 2 楼 t101lian 的回复:] 版主呢, 小当家呢,,坐等各位大牛
用递归可以的,就是用with来写递归就行。[/quote] 但是这个返佣金额算起来真麻烦,搞了好久还是不对。。 先去吃饭了,小当家你有时间的话帮我写下吧,我再另开一帖加分
LongRui888 2014-04-03
  • 打赏
  • 举报
回复
引用 2 楼 t101lian 的回复:
版主呢, 小当家呢,,坐等各位大牛
用递归可以的,就是用with来写递归就行。
唐诗三百首 2014-04-03
  • 打赏
  • 举报
回复
引用 楼主 t101lian 的回复:
返佣计算方法 ------------------------------------------------------------ | 合同一 (ID为1的那一行) 合同二 | 交易金额 100 (买家和卖家都要计算) 100 | 代理姓名 返佣金额 | 华东代理 60 | 100*0.6-100*0.6*0.5 100*0.6-100*0.6*0.5 上海代理 54 | 100*0.6*0.5 100*0.6*0.5-100*0.6*0.5*0.2 上海浦东代理 6 | 0 100*0.6*0.5*0.2
唐诗不才,这一段没有看到算法.请LZ讲解一下..
t101lian 2014-04-03
  • 打赏
  • 举报
回复
版主呢, 小当家呢,,坐等各位大牛
t101lian 2014-04-03
  • 打赏
  • 举报
回复
帖子已是最大分数, 不能加了?
t101lian 2014-04-03
  • 打赏
  • 举报
回复
t101lian 2014-04-03
  • 打赏
  • 举报
回复
引用 7 楼 fredrickhu 的回复:
[quote=引用 6 楼 t101lian 的回复:] [quote=引用 5 楼 fredrickhu 的回复:] 是不是你后面问的就是这个 问题啊?
跟这个有关系,弄这问题的过程中碰到那个问题,卡住了很久。。 版主大牛, 你也帮我看看这个问题吧[/quote] 你这个是力气活 交给他们来做吧 就小当家来做吧。 [/quote] 版主抛弃我们这些平民了!
--小F-- 2014-04-03
  • 打赏
  • 举报
回复
引用 6 楼 t101lian 的回复:
[quote=引用 5 楼 fredrickhu 的回复:] 是不是你后面问的就是这个 问题啊?
跟这个有关系,弄这问题的过程中碰到那个问题,卡住了很久。。 版主大牛, 你也帮我看看这个问题吧[/quote] 你这个是力气活 交给他们来做吧 就小当家来做吧。
t101lian 2014-04-03
  • 打赏
  • 举报
回复
引用 5 楼 fredrickhu 的回复:
是不是你后面问的就是这个 问题啊?
跟这个有关系,弄这问题的过程中碰到那个问题,卡住了很久。。 版主大牛, 你也帮我看看这个问题吧
--小F-- 2014-04-03
  • 打赏
  • 举报
回复
是不是你后面问的就是这个 问题啊?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧