Using Left Join based on date tables condition so that know who employees is absent

大城市里の小女人 提交于 2019-12-11 04:39:33

问题


I have two table From microsoft Access Database like this

1.HR_Personnel

+-----+----------+----------------------+
| 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   | 
+---------+-----------------------+

Expected Result EDIT

+-------+-----------------+-------------+-------------+-------------+
| 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  | -           | -           |
+-------+-----------------+-------------+-------------+-------------+

I have try with this Query EDIT

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

but the results display like this EDIT

+-------+-----------------+-------------+-------------+-------------+
| 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    |
+-------+-----------------+-------------+-------------+-------------+

I think the results of my query as it caused by i left join using

ON p.ID=a.Per_Code

EDIT so teguh are not present at 2013-02-21 date is not displayed. Table only shows Dhani Amanda who was absent from the date 2013-02-22 - 2013-02-22 in overall.

I just wanted to show employee roomates table is not present in all the particular date or Dates marked with Intime and OutTime empty

Finnally what must i change from my query? I hope you can help me. thanks.

UPDATE

I was wrong.I write upside down between the table result and expected result table.And then the result table not present Teguh as employees who are not present.I have change my explain. And then in the query

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

change by

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

I have edit my question with EDIT tag. thanks.


回答1:


You've got this line of code: Format (a.Date_Time, 'yyyy-mm-dd') as adate,

And you're expecting to get a date for a person who has no dates associated with them in table a. The only way that's going to work is if you create a new query (or SELECT statement) that selects the unique dates from table a, and NOT JOIN that back to table p.

Try running this, and then see if you can build the rest of your query off it. I'm a little swamped at work, but you should be able to take it over the finish line.

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;

I have a separate query called QueryDate which consists of:

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;

You can probably combine the two if you try hard enough.




回答2:


Problem Has Solve.

This Code show employee is not present in spesific Dates or ALL Dates marked with Intime and OutTime empty.

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;

Credit to johnny-bones who want to take the time to solve my problem.



来源:https://stackoverflow.com/questions/18353581/using-left-join-based-on-date-tables-condition-so-that-know-who-employees-is-abs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!