日期:2014-05-17  浏览次数:20635 次

sql 实现业绩分配,求达人解决
本帖最后由 ctmb520 于 2013-04-09 11:00:35 编辑
sql 实现业绩分配,有两个表分别为销售订单表A,用户表B

表A
订单总额(SumPrice) 运费(Price) 下单人员(UserID)  部门(DeptID) 介绍人员(RateUserID) 分配比率(Rate)
-----------------------------------------
900                              100          3                1010           15                    0.30
300                              0         8                1011           0                     0
800                                  200          14               1012           0                     0
用户表B
UserID     DeptID
---------------------------
3          1010
4          1010
8          1011
15         1012
14         1012
求按部门统计业绩分配结果

DeptID        销售总额
-----------------------------
1010          700=(900+100)*(1-0.3)
1011          300
1012          1300=(900+100)*0.3+(800+200)

第二个问题,对于业绩分配这个如何设计表的结构更合理?
SQL 业绩分配,数据统计

------解决方案--------------------
先左连接,然后group by,再计算
设计的还可以

------解决方案--------------------
select aa.deptid,aa.count1+bb.count2
(select b.deptid,(a.SumPrice+a.Price)*a.Rate as count1 from 销售订单表 a,用户表 b where a.RateUserID=b.UserID group by b.deptid) aa
,
(select a.deptid,(a.SumPrice+a.Price)*(1-a.Rate) as count2 from 销售订单表 a group by a.deptid) bb
where aa.deptid=bb.deptid
------解决方案--------------------
use Tempdb
go
--> --> 
 
declare @A table([SumPrice] int,[Price] int,[UserID] int,[DeptID] int,[RateUserID] int,[Rate] decimal(18,2))