1

我有两张microsoft Access Database这样的桌子

1.HR_人事

+-----+----------+----------------------+
| ID  | NIP      | Name                 |
+----------------+----------------------+
| 1   | 200      | Teguh                | 
| 2   | 201      | Supomo               | 
| 3   | 203      | DHINI ADHITYAS M     | 
| 4   | 204      | Dhani Amanda         | 
+-----+----------+----------------------+

TA_Record_Info

+---------+-----------------------+
| Per_Code| Date_Time             |
+---------+-----------------------+
| 3       | 2013-02-20 07:45:57   | 
| 2       | 2013-02-20 07:46:13   | 
| 1       | 2013-02-20 08:48:07   | 
| 1       | 2013-02-20 15:53:40   | 
| 3       | 2013-02-20 16:01:02   | 
| 2       | 2013-02-21 07:31:57   | 
| 3       | 2013-02-21 07:39:29   | 
| 3       | 2013-02-21 15:51:47   | 
| 2       | 2013-02-21 16:11:21   | 
| 2       | 2013-02-22 07:47:45   | 
| 1       | 2013-02-22 07:53:31   | 
| 3       | 2013-02-22 16:01:43   | 
| 2       | 2013-02-22 16:11:19   | 
| 1       | 2013-02-22 16:15:26   | 
+---------+-----------------------+

预期结果编辑

+-------+-----------------+-------------+-------------+-------------+
| NIP   | Nama            | adate       | InTime      | OutTime     |
+-------+-----------------+-------------+-------------+-------------+
| 201   | Teguh           | 2013-02-20  | 08:48:07    | 15:53:40    |
| 202   | Supomo          | 2013-02-20  | 07:46:13    | -           | 
| 203   | DHINI ADHITYAS M| 2013-02-20  | 07:45:57    | 16:01:02    |
| 204   | Dhani Amanda    | 2013-02-20  | -           | -           |
| 201   | Teguh           | 2013-02-21  | -           | -           |
| 202   | Supomo          | 2013-02-21  | 07:31:57    | 16:11:21    | 
| 203   | DHINI ADHITYAS M| 2012-08-21  | 07:39:29    | 15:51:47    |
| 204   | Dhani Amanda    | 2013-02-21  | -           | -           |
| 201   | Teguh           | 2012-08-22  | 07:53:31    | 16:15:26    |
| 202   | Supomo          | 2012-08-22  | 07:47:45    | 16:11:19    | 
| 203   | DHINI ADHITYAS M| 2012-08-22  | -           | 16:01:43    |
| 204   | Dhani Amanda    | 2013-02-22  | -           | -           |
+-------+-----------------+-------------+-------------+-------------+

我试过这个查询编辑

SELECT p.NIP AS NIP,
        p.Name AS Nama,
                                Format (a.Date_Time, 'yyyy-mm-dd') as adate,
                                IIF((Min(a.Date_Time) <> Max(a.Date_Time)),
                                    Format (Min(a.Date_Time), 'hh:mm:ss'),
                                    IIF( Format (Min(a.Date_Time), 'hh:mm:ss') < '12:00:00',
                                         Format (Min(a.Date_Time), 'hh:mm:ss'), 
                                        '-'
                                       )
                                   )as InTime,
                                IIF((Max(a.Date_Time) <> Min(a.Date_Time)),
                                    Format (Max(a.Date_Time), 'hh:mm:ss'),
                                    IIF( Format (Max(a.Date_Time), 'hh:mm:ss') > '12:00:00',
                                         Format (Max(a.Date_Time), 'hh:mm:ss'), 
                                         '-'
                                       )
                                    )as OutTime
                                FROM HR_Personnel AS p 
                                LEFT JOIN TA_Record_Info a 
                                ON p.ID = a.Per_Code  
                                GROUP BY p.Per_Code,
                                p.Per_Name,
                                         Format (a.Date_Time, 'yyyy-mm-dd')
                                Order BY Format (a.Date_Time, 'yyyy-mm-dd'),
                                            Right(p.Per_Code,2),
                                            p.Per_Name

但结果显示如下编辑

+-------+-----------------+-------------+-------------+-------------+
| NIP   | Nama            | adate       | InTime      | OutTime     |
+-------+-----------------+-------------+-------------+-------------+
| 204   | Dhani Amanda    |             |             | -           |
| 201   | Teguh           | 2013-02-20  | 08:48:07    | 15:53:40    |
| 202   | Supomo          | 2013-02-20  | 07:46:13    | -           | 
| 203   | DHINI ADHITYAS M| 2013-02-20  | 07:45:57    | 16:01:02    |
| 202   | Supomo          | 2013-02-21  | 07:31:57    | 16:11:21    | 
| 203   | DHINI ADHITYAS M| 2012-08-21  | 07:39:29    | 15:51:47    |
| 201   | Teguh           | 2012-08-22  | 07:53:31    | 16:15:26    |
| 202   | Supomo          | 2012-08-22  | 07:47:45    | 16:11:19    | 
| 203   | DHINI ADHITYAS M| 2012-08-22  | -           | 16:01:43    |
+-------+-----------------+-------------+-------------+-------------+

我认为我的查询结果是由我离开加入使用引起的

ON p.ID=a.Per_Code

编辑 所以 teguh 不在 2013-02-21 日期不显示。表格仅显示了在 2013-02-22 - 2013-02-22 日期期间缺席的 Dhani Amanda。

我只是想显示员工室友表不存在于所有特定日期或标记为 Intime 和 OutTime 为空的日期

最后我必须从我的查询中改变什么?我希望你能帮助我。谢谢。

更新

我错了。我在表结果和预期结果表之间颠倒写了。然后结果表作为不存在Teguh的员工不存在。我改变了我的解释。然后在查询中

SELECT p.NIP AS NIP,
        p.Name AS Nama,

改变

SELECT p.NIP AS NIP,
        p.Name AS Nama,

我已经用EDIT标签编辑了我的问题。谢谢。

4

2 回答 2

1

你有这行代码: Format (a.Date_Time, 'yyyy-mm-dd') as adate,

并且您期望在表 a 中为一个没有与他们关联的日期的人获得一个日期。唯一可行的方法是创建一个新查询(或 SELECT 语句),从表 a 中选择唯一日期,而不是将其加入表 p。

尝试运行它,然后看看您是否可以构建其余的查询。我工作有点忙,但你应该可以把它带过终点线。

SELECT QueryDate.ID, QueryDate.NIP, QueryDate.nama, QueryDate.adate, IIf((Min(TA_Record_Info.Date_Time)<>Max(TA_Record_Info.Date_Time)),Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss")<"12:00:00",Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),'-')) AS InTime, IIf((Max(TA_Record_Info.Date_Time)<>Min(TA_Record_Info.Date_Time)),Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss")>"12:00:00",Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss"),'-')) AS OutTime
FROM QueryDate LEFT JOIN TA_Record_Info ON (QueryDate.adate=DateValue(TA_Record_Info.Date_Time)) AND (QueryDate.ID=TA_Record_Info.Per_Code)
GROUP BY QueryDate.ID, QueryDate.NIP, QueryDate.nama, QueryDate.adate;

我有一个名为 QueryDate 的单独查询,其中包括:

SELECT HR_Personnel.ID, HR_Personnel.NIP, HR_Personnel.Name as nama, QDt.UniqueDate as adate
FROM HR_Personnel, (SELECT DateValue([Date_Time]) AS UniqueDate
FROM TA_Record_Info
GROUP BY DateValue([Date_Time])) AS QDt;

如果您足够努力,您可能可以将两者结合起来。

于 2013-08-21T16:41:44.720 回答
1

问题已解决。

此代码显示员工不存在于特定日期或标记为 Intime 和 OutTime 为空的所有日期。

SELECT QueryDate.HR_Personnel.ID, 
    QueryDate.HR_Personnel.Per_Code, 
    QueryDate.HR_Personnel.Per_Name, 
    QueryDate.QDt.UniqueDate, 
    IIf((Min(TA_Record_Info.Date_Time)<>Max(TA_Record_Info.Date_Time)),Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss")="",Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss")<"12:00:00",Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),'-'))) AS InTime,
    IIf((Max(TA_Record_Info.Date_Time)<>Min(TA_Record_Info.Date_Time)),Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss")="",Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),IIf(Format(Max(TA_Record_Info.Date_Time),"hh:mm:ss")>"12:00:00",Format(Min(TA_Record_Info.Date_Time),"hh:mm:ss"),'-'))) AS OutTimeFROM (
    SELECT HR_Personnel.ID, HR_Personnel.Per_Code, HR_Personnel.Per_Name, QDt.UniqueDate
    FROM HR_Personnel, (SELECT DateValue([Date_Time]) AS UniqueDate FROM TA_Record_Info GROUP BY DateValue([Date_Time])) AS QDt
)as QueryDate 
LEFT JOIN TA_Record_Info 
ON(QueryDate.QDt.UniqueDate=DateValue(TA_Record_Info.Date_Time)) AND(QueryDate.HR_Personnel.ID=TA_Record_Info.Per_Code)
GROUP BY QueryDate.HR_Personnel.ID, 
    QueryDate.HR_Personnel.Per_Code, 
    QueryDate.HR_Personnel.Per_Name, 
    QueryDate.QDt.UniqueDate
ORDER BY QueryDate.QDt.UniqueDate;

感谢想要花时间解决我的问题的johnny -bones 。

于 2013-08-22T03:57:21.557 回答