我有员工500人,现在我知道每人的年收入,怎么用随机函数把年收入分摊到每个月中,且每月工资不能相等,最终分配的工资应该等于年收入
如:我只知道 张三 年收入5W
我想得到的结果:
xm yf gz
张三 一月 3800
张三 二月 4500
张三 三月 3200
张三 四月 3500
张三 五月 4100
。。。。。
张三 十二月 4000
sum(gz)=5000
------解决思路----------------------
create table bp
(xm varchar(10),
yf varchar(10),
gz decimal(8,1))
declare @empname varchar(10), -- 姓名
@nsr decimal(8,1), -- 年收入
@i tinyint
select @empname='张三',
@nsr=50000,
@i=1
while(@i<=12)
begin
if @i<=11
begin
insert into bp(xm,yf,gz)
select @empname,
case @i when 1 then '一月' when 2 then '二月' when 3 then '三月'
when 4 then '四月' when 5 then '五月' when 6 then '六月'
when 7 then '七月' when 8 then '八月' when 9 then '九月'
when 10 then '十月' when 11 then '十一月' when 12 then '十二月' end,
cast(@nsr/12*0.9+rand()*@nsr/12*0.2 as decimal(8,1))
end
if @i=12
begin
insert into bp(xm,yf,gz)
select @empname,
'十二月',
@nsr-(select sum(gz) from bp where xm=@empname)
end
select @i=@i+1
end
-- 结果
select * from bp
/*
xm yf gz
---------- ---------- -----------------
张三 一月 3952.1
张三 二月 4511.3
张三 三月 3847.5
张三 四月 4258.7
张三 五月 4469.5
张三 六月 4192.0
张三 七月 4289.5
张三 八月 4376.8
张三 九月 4550.5
张三 十月 4439.3
张三 十一月 4188.2
张三 十二月 2924.6
(12 row(s) affected)
*/
-- 检验
select xm,
sum(gz) 'gz'
from bp
group by xm
/*
xm gz
---------- --------------
张三 50000.0
(1 row(s) affected)
*/