22,210
社区成员
发帖
与我相关
我的任务
分享
ID Name Gongzuo Tiqu
----------- ---------- ---------------------- -----------------
1 刘 3 1.5
2 刘 2 1
3 刘 1 0.5
4 张 1 0.5
5 张 2 1
6 王 5 2.5
7 王 3 1.5
要求:每人的总提成不超过2个点,多于2点的计为2
ID Name Gongzuo Tiqu
----------- ---------- ---------------------- -----------------
1 刘 3 1.5
2 刘 2 0.5 //1.5=2-0.5
3 刘 1 0 //已超过22分,计为0
4 张 1 0.5
5 张 2 1
6 王 5 2 //2.5>2,最多为2分
7 王 3 0 //已超过22分,计为0
--写的很恶心,大家轻点拍
--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:[TB]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]')
AND type in (N'U')) --U 代表你查询的是表
DROP TABLE [TB]
GO
---->建表
create table [TB]([name] varchar(5),[num] int,[tiqu] numeric(2,1))
insert [TB]
select 'liu',3,1.5 union all
select 'liu',2,1 union all
select 'liu',1,0.5 union all
select 'zhang',1,0.5 union all
select 'zhang',2,1 union all
select 'wa',1,0.5 union all
select 'wa',2,0.6 union all
select 'wa',3,0.7 union all
select 'wa',4,0.8 union all
select 'wa',5,0.9 union all
select 'wa',6,1.1 union all
select 'sun',5,2.5 union all
select 'sun',3,1.5
GO
--以下脚本分批执行
--添加几个标志位: 标志ID,合计值,差值,是否超2标志
alter table [TB]
add id int,totle numeric(2,1),dif numeric(2,1),totleflag int
go
--更新标志位
declare @name varchar(20)
declare @num int
declare @t numeric(2,1)
declare @tot numeric(2,1)
declare @dif numeric(2,1)
declare @totleflag int
set @name=''
set @t=0.0
set @num=0
update TB
set totle = @tot
,@tot= case when @name=name then @tot+tiqu else tiqu end
,@dif= case when @name=name then @tot-2 else tiqu-2 end
,@num= case when @name=name then @num+1 else 0 end
,@name=name
,id=@num
,dif=@dif
,@totleflag = case when @dif>=tiqu then 1 else 0 end
,totleflag = @totleflag
go
--检查表数据
SELECT * FROM [TB]
--修改提取值
update TB
set
tiqu = case when dif <0 and totleflag=0 then tiqu
when dif >0 and totleflag =0 then tiqu-dif
when dif >0 and totleflag =1 then 0
end
--最后取值
SELECT name ,num, tiqu FROM [TB]
--> 删除表格
--DROP TABLE [TB]