0

I have attendance record table like this

+----------------+---------+-----------------------+
| NIP            | Nama    | Date_Time             |
+----------------+---------+-----------------------+
| 050803075201   | Supomo  | 2013-02-20 07:45:57   | 
| 050803075201   | Supomo  | 2013-02-20 17:24:13   | 
| 050803075201   | Supomo  | 2013-02-21 07:53:40   | 
| 050803075201   | Supomo  | 2013-02-21 17:31:57   | 
| 050803075200   | Teguh   | 2013-02-21 20:31:02   | 
| 050803075200   | Teguh   | 2013-02-20 18:18:07   | 
+----------------+---------+-----------------------+

Date_Time is in string format.

And then I want to make it like this table:

+----------------+---------+-------------+-------------+-------------+
| NIP            | Nama    | Date        | In          | Out         |
+----------------+---------+-------------+-------------+-------------+
| 050803075200   | Teguh   | 2013-02-21  |             | 18:18:07    |
| 050803075200   | Teguh   | 2013-02-20  |             | 20:31:02    | 
| 050803075201   | Supomo  | 2013-02-20  | 07:45:57    | 17:24:13    | 
| 050803075201   | Supomo  | 2013-02-21  | 07:53:40    | 17:31:57    |
+----------------+---------+-------------+-------------+-------------+

What query will do this?

4

1 回答 1

0

Here is the SQLFiddel Demo

Below is the Query

select NIP,
       Nama,
       DATE_FORMAT(Date_Time, '%Y-%m-%d') as date,
       Case Min(Date_Time) 
         When Max(Date_Time) 
          Then '' 
         Else DATE_FORMAT(Date_Time, '%H:%i:%s') 
       End as InTime,

       DATE_FORMAT(max(Date_Time), '%H:%i:%s') as OutTime
  from Attd
 Group By NIP,
       Nama,
       DATE_FORMAT(Date_Time, '%Y-%m-%d')
于 2013-08-14T09:33:55.673 回答