0

I am a little new to building this but have come a long way.

I have built a db using Access 2007. I have a table that shows the employees info:

Lname  
Fname  
Status  
HireDate  
TermDate

(Status: they are either inactive (potential Hires), Active or Terminated)

I can run a query that will show me all the employees by hire date or run one to show term dates.

We would like to have a query that will give us a count of how many drivers are still there within a given month.

Say Joe Smith was hired on 01/01/2008 and was terminated on 05/15/2011. If I ran a report in 2011 on May 31st how would I need to build the query to show this employee as being there in the month of May?

I have used >=Date() and others. I could use between #05/01/2011# and #06/01/2011# in the criteria under TermDate but if there is not a date there, nothing shows up. I have even dropped down a line and added "Null" and still nothing or I get all the employees that are still there and the ones that was terminated before the dates. I'm not sure what I am doing wrong.

4

1 回答 1

1

我不确定这个过滤条件的逻辑。我认为您的目标是确定在 2011 年 5 月的任何时候都在工作的所有司机。我最好的猜测是您至少需要 2 个条件来识别他们。

  • HireDate2011 年 6 月 1 日之前
  • TermDateNull 或 >= 2011 年 5 月 1 日

如果这些条件足够,SQL 可能会相当简单。

SELECT e.Lname, e.Fname
FROM employees_info AS e
WHERE
    e.HireDate < #2011-6-1#
    AND
    (
        e.TermDate Is Null
        OR
        e.TermDate >= #2011-5-1#
    );

听起来您正在设计视图中构建查询......这是一个很好且有用的功能。但是,很难描述如何在设计视图中构建该查询。所以我建议您创建一个新查询,切换到 SQL 视图并粘贴该 SQL 文本。替换employees_info为您的实际表名,并修复我拼写错误的所有字段名。

如果该查询运行没有错误,您可以在设计视图和 SQL 视图之间来回切换,在一个视图中进行更改,然后检查它在另一个视图中的表示方式。

SQL 不必按照我编写的方式进行格式化。我选择了这种方式,希望它能使WHERE逻辑清晰。如果您从设计视图更改查询,Access 将重新格式化 SQL,因为它认为合适。但是,格式更改不应中断查询。

我对文字日期值使用了yyyy-md格式。该格式避免了在哪些部分代表日期和月份方面可能出现的任何混淆,例如#05-01-2011#是打算代表 5 月 1 日还是 1 月 5 日。但是,当您更改查询时,Access 可能会将它们更改为mm-dd-yyyy格式。(有时它的“有用”冲动很烦人。)

我对某一点感到困惑。似乎每个员工都有一份记录。如果是这样,并且员工可以出于任何原因离职并在以后重新雇用,那么很难在单个记录中捕获不同的雇佣条款。如果您面临这种情况,您可能需要修改表格设计。

如果我误解了您的数据,请向我们展示一个简短的数据样本,以及您希望从基于该样本的查询中获得的输出。祝你好运。

于 2013-04-06T15:23:12.707 回答