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

做了个计件工资系统,统计的时候遇到了点麻烦求高手
计件工资系统做了个,统计遇到了麻烦。问题如下

某个产品设置多个工段,这是随机的,有可能只有2档,有可能有5档工资,
我这里假设3档工资,插入数据库表里作为3条记录。
字段1是大于等于值,字段2是小于的值。
例如大于等于0小于200,单价为1元
大于等于200小于500,单价为1.5,大于等于500,单价为2元

现在录入一批工人的工作数量,例如100条记录,个数从100-1000不等

如何能计算出总工资。在这些都是随机的情况下

------解决方案--------------------

create table #item(v1 int,v2 int ,price decimal(12,2))
insert into #item
select 0,200,1
union all select 200,500,1.5
union all select 500,1000,2

create table #tb(empl_no varchar(10),num int)
insert into #tb
select '001',300
union all select '002',100
union all select '003',600
union all select '002',800
union all select '003',100
union all select '001',500
union all select '003',800

select * from #item
select * from #tb

select empl_no,sum(salary) as salary
from (
select *,salary=(select top 1 price from #item b where a.num>=v1 and a.num<v2)
from #tb a
)t
group by empl_no

drop table #item,#tb

/*
empl_no salary
--------------------------
001 3.50
002 3.00
003 5.00
*/


这是不是你想要的结果?

------解决方案--------------------
你用case  when语句

SQL



Case

的使用方法

   

Case

具有两种格式。简单

Case

函数和

Case

搜索函数。

 

--

简单

Case

函数

 

CASE

 sex   

    

   

  

WHEN

 

'1'

 

THEN

 

'



'

 

 

    

   

  

WHEN