2013-07-10 18 views
0

我有兩個表。他們目前在Access 2010中,但最終會遷移到Microsoft SQL 2010 DBMS。一個叫做EmployeeWages,另一個叫TimeClock。 EmployeeWages包含員工ID,工資和生效日期。 TimeClock表具有員工ID,與員工相關的工資以及工資有效的日期。匹配一個表的條目與另一個表中的兩個日期之間的數據點

我需要做的是匹配僱員工作的正確工資。目前該表具有TimeIn和TimeOut字段。爲了簡單起見,在這個問題中,我已經計算了工作時間,但是如果我可以在一個查詢中獲得所有內容,那將非常棒。

EmployeeWage 
---------------------------------------------------------------------- 
| EmployeeID | RecordNum | EffectiveDate | WorkType | Wage | 
-+------------+-+-----------+-+---------------+-+----------+-+------+- 
| 6240  |  1  |  1/18/13  | Line | 6 | 
---------------------------------------------------------------------- 
| 6240  |  2  |  4/1/13  | Bench | 7 | 
---------------------------------------------------------------------- 
| 6240  |  3  |  6/1/13  | Min  | 7.35 | 
---------------------------------------------------------------------- 
| 6240  |  4  |  6/23/13  | Bench | 5 | 
---------------------------------------------------------------------- 
| 6240  |  5  |  6/24/13  | Bench | 6.25 | 
---------------------------------------------------------------------- 
| 6240  |  5  |  6/25/13  | Bench | 7.25 | 
---------------------------------------------------------------------- 

然後

TimeClock 
-------------------------------------------------- 
| EmployeeID | WorkType | Work Date| Hours | 
-+------------+-+----------+-+---------+-+------+- 
| 6240  | Line | 6/22/13 | 5 | 
-------------------------------------------------- 
| 6240  | Bench | 6/22/13 | 7 | 
-------------------------------------------------- 
| 6240  | Bench | 6/23/13 | 5 | 
-------------------------------------------------- 
| 6240  | Bench | 6/24/13 | 6.25 | 
-------------------------------------------------- 
| 6240  | Min  | 6/24/13 | 2 | 
-------------------------------------------------- 

產生的匹配表應該像...

TimeClock 
----------------------------------------------------------- 
| EmployeeID | WorkType | Work Date| Hours | Wage | 
-+------------+-+----------+-+---------+-+------+-+------+- 
| 6240  | Line | 6/22/13 | 5 | 6 | 
----------------------------------------------------------- 
| 6240  | Bench | 6/22/13 | 6.25 | 7 | 
----------------------------------------------------------- 
| 6240  | Bench | 6/23/13 | 6 | 5 | 
----------------------------------------------------------- 
| 6240  | Bench | 6/24/13 | 5.5 | 6.25 | 
----------------------------------------------------------- 
| 6240  | Min  | 6/24/13 | 2 | 7.35 | 
----------------------------------------------------------- 

任何幫助,使他們那裏我將不勝感激!

+0

你需要在加入一個額外條件,否則會產生(部分)Carthesian產品。 – wildplasser

回答

0
SELECT T.EmployeeID, T.WorkType, T.[Work Date], T.Hours, 
(SELECT Max(E.EffectiveDate) 
FROM EmployeeWage E 
WHERE T.EmployeeID = E.EmployeeID 
AND T.WorkType = E.WorkType 
AND E.EffectiveDate <= T.[Work Date]) AS ActiveDate, 
E.Wage as Wage 
FROM TimeClock T, EmployeeWage EW 
WHERE T.EmployeeID = EW.EmployeeID 
AND TW.WorkType = EW.WorkType 
AND E.EffectiveDate = ActiveDate 

請注意,我沒有MS-Access,也沒有嘗試過。如果有幫助或錯誤,請告訴我。

0

我不知道一個完美的方式與一個單一的查詢,讓您的結果,但是這會給你的基本信息,你需要:

SELECT TimeClock.EmployeeID, TimeClock.WorkType, TimeClock.WorkDate, EmployeeWage.EffectiveDate, TimeClock.Hours, EmployeeWage.Wage 
    FROM EmployeeWage INNER JOIN TimeClock 
    ON EmployeeWage.EmployeeID = TimeClock.EmployeeID 
    AND TimeClock.WorkType = EmployeeWage.WorkType 
    WHERE TimeClock.WorkDate >= EmployeeWage.EffectiveDate; 
+0

@shahkalpesh我不斷收到E.Wage,TW.WorkType,E.EctivectiveDate,ActiveDate的「輸入參數值」提示。我將E.Wage改爲EW.Wage,TW.WorkType改爲T.WorkType,E.EffectiveDate改爲EW.EffectiveDate。現在我所得到的是ActiveDate的提示... –

+0

是的,對不起,我在測試之前提出了自己的代碼,並在測試之後進行了編輯。請嘗試新的代碼,看看是否適合你。請注意,_WorkDate_可能在您的數據庫中不同。 – Sabe

相關問題