使用组合列为最近日期创建过滤器

时间:2014-12-08 19:11:02

标签: sql vba ms-access embedding

我在Access中创建了一个过滤应用程序,它引用了四个简单的表:

Employee: Emp_ID, FirstName, LastName 
Skill: Skill_ID, SkillName, SkillDescription, SkillGroup
Employee_Skill: Entry_ID, Emp_ID, Skill_ID, LevelofExperience, Dateupdated
SkillGroupName:SkillGroup_ID SkillGroupName`

基本上,这个数据库的概念是跟踪员工技能以及经验水平随着时间的推移如何改善(或不改善!)。我面临的问题是我希望应用程序按最近更新的技能和员工组合进行过滤。我找到了允许我将这两列用作不同实体的查询:

SELECT DISTINCT Emp_ID, Skill_ID FROM Employee_Skill
WHERE (SELECT MAX(DateUpdated)From Employee_Skill);

它完全独立运行,但我不知道如何将它合并到我的主查询中,它只是将必要的列连接在一起,以便更轻松地获得最终用户体验。它没有明显显示Emp_IDSkill_ID。它也不适用于应用程序的VBA。 (-1 =包括所有历史记录; 0 =仅包括最新更新。)

更新: 通过我的主要查询,我已经能够通过这样做选择Employee和Skill的独特组合:

SELECT 
 Employee.FirstName, 
 Employee.LastName, 
 Max(Employee_Skill.LevelOfExperience) AS LevelOfExperience, 
 Skill.SkillName, 
 Max(Employee_Skill.DateUpdated) AS DateUpdated, 
 Max(SkillGroup.SkillGroupName) AS SkillGroupName
FROM 
 SkillGroup INNER JOIN 
 (Skill INNER JOIN 
  (Employee INNER JOIN 
   Employee_Skill ON 
   Employee.Emp_ID = Employee_Skill.Emp_ID) ON 
  Skill.Skill_ID = Employee_Skill.Skill_ID) ON 
 SkillGroup.SkillGroup_ID = Skill.SkillGroup
WHERE 
 Employee.Active=True
GROUP BY 
 Employee.FirstName, 
 Employee.LastName, 
 Skill.SkillName
ORDER BY 
 Max(Employee_Skill.LevelOfExperience) DESC;

但是,我在此查询上构建的表单和报表只能看到最新版本的选项。我真的希望有一个动态表单,可以根据需要删除约束。

1 个答案:

答案 0 :(得分:0)

不确定您使用Max(Employee_Skill.LevelOfExperience)Max(SkillGroup.SkillGroupName)做了什么,但我认为您需要坚持查询详细信息行,然后添加标记Max(Employee_Skill.DateUpdated)过滤器的其他列,例如:

SELECT 
 Employee.FirstName, 
 Employee.LastName, 
 Employee_Skill.LevelOfExperience, 
 Skill.SkillName, 
 Employee_Skill.DateUpdated, 
 SkillGroup.SkillGroupName,
 iif(max_dateUpdated=dateupdated,1,0) as is_max_DateUpdated
FROM 
 SkillGroup INNER JOIN 
 (Skill INNER JOIN 
  (Employee INNER JOIN 
   Employee_Skill ON 
   Employee.Emp_ID = Employee_Skill.Emp_ID) ON 
  Skill.Skill_ID = Employee_Skill.Skill_ID) ON 
 SkillGroup.SkillGroup_ID = Skill.SkillGroup inner join
 (select  
   empID,
   max(dateupdated) as max_dateUpdated 
  from
   Employee_Skill
  group by 
   empID) mx on
  Employee.empID = mx.empID
WHERE 
 Employee.Active=True