DateTime String to Date, In Time and Out Time

空扰寡人 提交于 2019-12-25 05:37:20

问题


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?


回答1:


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')


来源:https://stackoverflow.com/questions/18227191/datetime-string-to-date-in-time-and-out-time

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