我有两张桌子。它们目前位于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 :(得分: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,也没有尝试过。如果有帮助或错误,请告诉我。
答案 1 :(得分: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;