2012-08-26 39 views
1

使用SQL Server 2000如何扣除額的時間條件

表1

ID Salary (Monthly) perday (salary) 

001 3000 100 
002 1500 50 
003 4500 150 

Salaryperday列的數據類型的float

表2

ID Date Latetime (HH:mm) 

001 01/02/2012 00:15 
001 02/02/2012 00:10 
001 03/02/2012 00:45 
001 04/02/2012 00:29 
001 05/02/2012 01:00 

002 11/03/2012 00:02 
002 12/03/2012 00:20 
002 13/03/2012 00:29 
002 14/03/2012 01:00 


002 10/03/2012 01:30 
002 10/03/2012 02:00 

我想按照扣除工資金額晚點的數量。

條件

01至29 mintues後期條件

  • 如果用戶後期在1日的時間不扣除
  • 如果用戶下旬第二次perday工資
  • 的10%抵扣當用戶後期第3次25%扣除日薪工資
  • 如果用戶第4次及以上扣除工資的50%扣除額

30個mintues至1小時晚條件

  • 如果用戶遲到第一時間沒有扣
  • 如果用戶遲到第二時間perday薪水的50%扣除
  • 如果用戶遲到第三時間100 perday薪水
  • 的%扣除如果用戶晚於第四時間以上perday薪水

期望輸出FO的150%扣除從表2表2 - [R

  4th onwards 
ID Ist 2nd 3rd days Amount Deducted 


001 0 10 50 2 250 310 
002 0 10 10 1 100 120 

輸出解釋

用戶001遲到5次爲一計數(latetime)

    爲00:15分鐘
  • 第一時間晚 - 無扣
  • 第二時間晚了00:10分鐘,所以遲到時間在01到29之間,所以每天工資扣除的10%'第一次(01到29)
  • 第三次晚上00:45分鐘,所以在晚些時候下注因爲30到01小時,所以每天工資扣除的50%'第一次(30到01)
  • 第4次晚上00:29分鐘,所以遲到時間在01到29之間,所以每天工資扣除50%第二時間(01至29)
  • 爲01:00分鐘,在30至01小時,使遲時間,perday扣錢「第一時間(30至01)

所以150%第五時間晚如何爲上述條件創建查詢?

+0

不確定SqlServer(T-SQL)是否是這類工作的最佳工具。這似乎是商業邏輯的決定。 – Steve

+0

在插入table2之後或之前使用觸發器會很容易。你同意嗎?如果是,那麼可以有一個簡單的解決方案 – Sami

回答

3
--create and populate penalty rules 
create table table4(latetype int null, nthtime int null, mulfactor float null) 
insert into table4 values (1,1,0) insert into table4 values (1,2,0.1) 
insert into table4 values (1,3,0.25) insert into table4 values (1,4,0.5) 
insert into table4 values (2,1,0) insert into table4 values (2,2,0.5) 
insert into table4 values (2,3,1.0) insert into table4 values (2,4,1.5) 
--create third table to populate the nthtime and latetype for table2 
select x.id, date, 
     (select count(*) from table2 where id=x.id and date<=x.date) as nthtime, 
     case when x.latetime<'00:30' then 1 else 2 end as latetype 
into table3 
from table2 x join table1 on table1.id = x.id 
--select the deduction amounth per id 
select table1.id, 
     sum(table1.perday* 
     isnull(mulfactor, case when latetype = 1 then .5 else 1.5 end))as deduction 
from table3 a 
left join table4 b on a.latetype=b.latetype and a.nthtime=b.nthtime 
join table1 on table3.id = table1.id 
group by table1.id