我有一个培训数据库,我将所有数据加载到培训历史记录表中。大部分培训定期重复,我使用查询刷新主题表,仅保留该主题的最新培训信息。我想用SQL替换这些Access查询,但不能正确获取语法(自学)。以下是来自其中一个查询的SQL视图,该查询提取每个员工的员工ID,员工姓名和最近的叉车培训日期,并将其放入叉车表中。 (它实际上比这更复杂,或者我只是更新培训日期,但我删除了查询,因此不会太混乱。)
所以我只需要知道如何修改这段代码,以便它可以作为SQL命令使用。
INSERT INTO Forklift_tbl ( EmpID, FullName, ForkliftTrainDate )
SELECT TrainHist_tbl.[EmpID]
, TrainHist_tbl.[FullName]
, Max(TrainHist_tbl.[TrainDate]) AS MaxOfTrainDate
FROM TrainHist_tbl
GROUP BY TrainHist_tbl.[EmpID]
, TrainHist_tbl.[FullName]
, TrainHist_tbl.[CourseName]
HAVING (((TrainHist_tbl.[CourseName])= 'Forklift Operator Training'));
数据表TrainHist:
EmpID| FullName | CourseName| TrainDate
123| Doe, John | Forklift| 1/1/16
123| Doe, John | AED| 2/2/16
123| Doe, John | Forklift| 2/6/17
234| Smith, Bob| Forklift| 1/2/16
234| Smith, Bob| AED| 2/2/16
234| Smith, Bob| Forklift| 3/7/17
结果插入叉车表
123| Doe, John| Forklift| 2/6/17
234| Smith, Bob| Forklift| 3/7/17
答案 0 :(得分:0)
尝试使用此简化查询:
INSERT INTO Forklift_tbl ( EmpID, FullName, ForkliftTrainDate )
SELECT TrainHist_tbl.[EmpID]
, TrainHist_tbl.[FullName]
, Max(TrainHist_tbl.[TrainDate]) AS MaxOfTrainDate
FROM TrainHist_tbl
WHERE TrainHist_tbl.[CourseName] = 'Forklift Operator Training'
GROUP BY TrainHist_tbl.[EmpID]
, TrainHist_tbl.[FullName];