Time attendant program
I have table T_Personnel like this
+----+------+----------+---------+
| ID | NIK | Name | Dept_ID |
+----+------+----------+---------+
| 1 | 1001 | Ann | 1 |
| 2 | 1002 | Bond | 1 |
| 3 | 1003 | Chan | 2 |
| 4 | 1004 | Dea | 2 |
| 5 | 1005 | Eno | 3 |
+----+------+----------+---------+
And table T_TimeAttendant
+----+------+---------------------+--------+
| ID | NIK | DateTime | In_Out |
+----+---- -+---------------------+--------+
| 1 | 1001 | 01/10/2013 7:30:01 | 1 |
| 2 | 1002 | 01/10/2013 7:31:05 | 1 |
| 3 | 1003 | 01/10/2013 7:32:21 | 1 |
| 4 | 1004 | 01/10/2013 7:45:16 | 1 |
| 5 | 1001 | 01/10/2013 17:00:47 | 2 |
| 6 | 1002 | 01/10/2013 17:02:01 | 2 |
| 7 | 1003 | 01/10/2013 17:03:22 | 2 |
| 8 | 1001 | 02/10/2013 7:29:01 | 1 |
| 9 | 1002 | 02/10/2013 7:34:37 | 1 |
| 10 | 1001 | 02/10/2013 17:05:16 | 2 |
| 11 | 1002 | 02/10/2013 17:08:28 | 2 |
+----+------+---------------------+--------+
My query is
SELECT Tmp.P.NIK, Max(Tmp.P.Name) AS Name, Tmp.Date_IO, Max(Tmp.TimeIn_1) AS Time_In, Max(Tmp.TimeOut_1) AS Time_Out
FROM [SELECT P.NIK, P.Name, LEFT(TA.DateTime,10) AS Date_IO, RIGHT(TA.DateTime,8) AS TimeIn_1, ' ' AS TimeOut_1
FROM T_Personnel AS P LEFT JOIN T_TimeAttendant AS TA ON P.NIK=TA.NIK
WHERE TA.In_Out = 1 OR TA.In_Out IS NULL
UNION ALL
SELECT P.NIK, P.Name, LEFT(TA.DateTime,10) AS Date_IO, ' ' AS TimeIn_1, RIGHT(TA.DateTime,8) AS TimeOut_1
FROM T_Personnel AS P LEFT JOIN T_TimeAttendant AS TA ON P.NIK=TA.NIK
WHERE TA.In_Out = 2 OR TA.In_Out IS NULL
]. AS Tmp
GROUP BY Tmp.Date_IO, Tmp.P.NIK;
The result
+------+-------+------------+---------+----------+
| NIK | Name | Date_IO | Time_In | Time_Out |
+------+-------+------------+---------+----------+
| 1005 | Eno | | | |
| 1001 | Ann | 01/10/2013 | 7:30:01 | 17:00:47 |
| 1002 | Bond | 01/10/2013 | 7:31:05 | 17:02:01 |
| 1003 | Chan | 01/10/2013 | 7:32:21 | 17:03:22 |
| 1004 | Dea | 01/10/2013 | 7:45:16 | |
| 1001 | Ann | 02/10/2013 | 7:29:01 | 17:05:16 |
| 1002 | Bond | 02/10/2013 | 7:34:37 | 17:08:28 |
+------+-------+------------+---------+----------+
How to make table like this ?
+------+-------+------------+---------+----------+
| NIK | Name | Date_IO | Time_In | Time_Out |
+------+-------+------------+---------+----------+
| 1001 | Ann | 01/10/2013 | 7:30:01 | 17:00:47 |
| 1002 | Bond | 01/10/2013 | 7:31:05 | 17:02:01 |
| 1003 | Chan | 01/10/2013 | 7:32:21 | 17:03:22 |
| 1004 | Dea | 01/10/2013 | 7:45:16 | |
| 1005 | Eno | 01/10/2013 | | |
| 1001 | Ann | 02/10/2013 | 7:29:01 | 17:05:16 |
| 1002 | Bond | 02/10/2013 | 7:34:37 | 17:08:28 |
| 1003 | Chan | 02/10/2013 | 7:31:05 | 17:02:01 |
| 1004 | Dea | 02/10/2013 | | |
| 1005 | Eno | 02/10/2013 | | |
+------+-------+------------+---------+----------+
Thanks before for the enlightenment
Btw I try to make it look good but auto trimming in this forum ruin the tables
Edit: Database file attached
I have table T_Personnel like this
+----+------+----------+---------+
| ID | NIK | Name | Dept_ID |
+----+------+----------+---------+
| 1 | 1001 | Ann | 1 |
| 2 | 1002 | Bond | 1 |
| 3 | 1003 | Chan | 2 |
| 4 | 1004 | Dea | 2 |
| 5 | 1005 | Eno | 3 |
+----+------+----------+---------+
And table T_TimeAttendant
+----+------+---------------------+--------+
| ID | NIK | DateTime | In_Out |
+----+---- -+---------------------+--------+
| 1 | 1001 | 01/10/2013 7:30:01 | 1 |
| 2 | 1002 | 01/10/2013 7:31:05 | 1 |
| 3 | 1003 | 01/10/2013 7:32:21 | 1 |
| 4 | 1004 | 01/10/2013 7:45:16 | 1 |
| 5 | 1001 | 01/10/2013 17:00:47 | 2 |
| 6 | 1002 | 01/10/2013 17:02:01 | 2 |
| 7 | 1003 | 01/10/2013 17:03:22 | 2 |
| 8 | 1001 | 02/10/2013 7:29:01 | 1 |
| 9 | 1002 | 02/10/2013 7:34:37 | 1 |
| 10 | 1001 | 02/10/2013 17:05:16 | 2 |
| 11 | 1002 | 02/10/2013 17:08:28 | 2 |
+----+------+---------------------+--------+
My query is
SELECT Tmp.P.NIK, Max(Tmp.P.Name) AS Name, Tmp.Date_IO, Max(Tmp.TimeIn_1) AS Time_In, Max(Tmp.TimeOut_1) AS Time_Out
FROM [SELECT P.NIK, P.Name, LEFT(TA.DateTime,10) AS Date_IO, RIGHT(TA.DateTime,8) AS TimeIn_1, ' ' AS TimeOut_1
FROM T_Personnel AS P LEFT JOIN T_TimeAttendant AS TA ON P.NIK=TA.NIK
WHERE TA.In_Out = 1 OR TA.In_Out IS NULL
UNION ALL
SELECT P.NIK, P.Name, LEFT(TA.DateTime,10) AS Date_IO, ' ' AS TimeIn_1, RIGHT(TA.DateTime,8) AS TimeOut_1
FROM T_Personnel AS P LEFT JOIN T_TimeAttendant AS TA ON P.NIK=TA.NIK
WHERE TA.In_Out = 2 OR TA.In_Out IS NULL
]. AS Tmp
GROUP BY Tmp.Date_IO, Tmp.P.NIK;
The result
+------+-------+------------+---------+----------+
| NIK | Name | Date_IO | Time_In | Time_Out |
+------+-------+------------+---------+----------+
| 1005 | Eno | | | |
| 1001 | Ann | 01/10/2013 | 7:30:01 | 17:00:47 |
| 1002 | Bond | 01/10/2013 | 7:31:05 | 17:02:01 |
| 1003 | Chan | 01/10/2013 | 7:32:21 | 17:03:22 |
| 1004 | Dea | 01/10/2013 | 7:45:16 | |
| 1001 | Ann | 02/10/2013 | 7:29:01 | 17:05:16 |
| 1002 | Bond | 02/10/2013 | 7:34:37 | 17:08:28 |
+------+-------+------------+---------+----------+
How to make table like this ?
+------+-------+------------+---------+----------+
| NIK | Name | Date_IO | Time_In | Time_Out |
+------+-------+------------+---------+----------+
| 1001 | Ann | 01/10/2013 | 7:30:01 | 17:00:47 |
| 1002 | Bond | 01/10/2013 | 7:31:05 | 17:02:01 |
| 1003 | Chan | 01/10/2013 | 7:32:21 | 17:03:22 |
| 1004 | Dea | 01/10/2013 | 7:45:16 | |
| 1005 | Eno | 01/10/2013 | | |
| 1001 | Ann | 02/10/2013 | 7:29:01 | 17:05:16 |
| 1002 | Bond | 02/10/2013 | 7:34:37 | 17:08:28 |
| 1003 | Chan | 02/10/2013 | 7:31:05 | 17:02:01 |
| 1004 | Dea | 02/10/2013 | | |
| 1005 | Eno | 02/10/2013 | | |
+------+-------+------------+---------+----------+
Thanks before for the enlightenment
Btw I try to make it look good but auto trimming in this forum ruin the tables
Edit: Database file attached
Attachments
Last edited: