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

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 (合同表的时间)

...全文
301 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
  • 打赏
  • 举报
回复
是不是你后面问的就是这个 问题啊?
1. C 语言中的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++中指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 13. 位域 58 14. C语言函数二维数组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函数大全 68 17. C语言宏定义技巧 89 18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函数与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函数使用大全 158 33. 二叉树的数据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI C中struct内存布局 173 36. 冒泡和选择排序实现 180 37. 函数指针数组与返回数组指针的函数 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与数组学习笔记 222 45. 数组不是指针 224 46. 标准C中字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函数的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高级C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机数的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员数据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450
目录(Table of Contents)   前言(Preface)   第一部分(Part I) 基础(Foundations)   第一章 计算中算法的角色(The Role of Algorithms in Computing)   第二章 开始(Getting Started)   第三章 函数的增长率(Growth of Functions)   第四章 递归(Recurrences)   第五章 概率分析与随机化算法(Probabilistic Analysis and Randomized Algorithms)   第二部分(Part II) 排序与顺序统计(Sorting and Order Statistics)   第六章 堆排序(Heapsort)   第七章 快速排序(Quicksort)   第八章 线性时间中的排序(Sorting in Linear Time)   第九章 中值与顺序统计(Medians and Order Statistics)   第三部分(Part III) 数据结构(Data Structures)   第十章 基本的数据结构(Elementary Data Structures)   第十一章 散列表(Hash Tables)   第十二章 二叉查找树(Binary Search Trees)   第十三章 红-黑树(Red-Black Trees)   第十四章 扩充的数据结构(Augmenting Data Structures)   第四部分(Part IV) 高级的设计与分析技术(Advanced Design and Analysis Techniques)   第十五章 动态规划(Dynamic Programming)   第十六章 贪婪算法(Greedy Algorithms)   第十七章 分摊分析(Amortized Analysis)   第五部分(Part V) 高级的数据结构(Advanced Data Structures)   第十八章 B-树(B-Trees)   第十九章 二项式堆(Binomial Heaps)   第二十章 斐波纳契堆(Fibonacci Heaps)   第二十一章 不相交集的数据结构(Data Structures for Disjoint Sets)   第六部分(Part VI) 图算法(Graph Algorithms)   第二十二章 基本的图算法(Elementary Graph Algorithms)   第二十三章 最小生成树(Minimum Spanning Trees)   第二十四章 单源最短路径(Single-Source Shortest Paths)   第二十五章 全对的最短路径(All-Pairs Shortest Paths)   第二十六章 最大流(Maximum Flow)   第七部分(Part VII) 精选的主题(Selected Topics)   第二十七章 排序网络(Sorting Networks)   第二十八章 矩阵运算(Matrix Operations)   第二十九章 线性规划(Linear Programming)   第三十章 多项式与快速傅里叶变换(Polynomials and the FFT)   第三十一章 数论算法(Number-Theoretic Algorithms)   第三十二章 字符串匹配(String Matching)   第三十三章 计算几何学(Computational Geometry)   第三十四章 NP-完备性(NP-Completeness)   第三十五章 近似算法(Approximation Algorithms)   第八部分(Part VIII) 附录:数学背景(Mathematical Background)   附录A 和(Summations)   附录B 集合,等等。(Sets, Etc.)   附录C 计数与概率(Counting and Probability)   参考文献(Bibliography)   索引(Index)

22,300

社区成员

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

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