我在每日时间记录中对数据进行分组和排序时遇到问题
自动生成报告。
DTR
的表结构如下:
Log Date Log Time Employee Name Log Type 9/13/2012 17:00:00 SALITA, LYNYRD ANTONIO LOGOUT 9/13/2012 8:05:00 SALITA, LYNYRD ANTONIO LOGIN 9/12/2012 17:05:00 SALITA, LYNYRD ANTONIO LOGOUT 9/12/2012 8:05:00 SALITA, LYNYRD ANTONIO LOGIN 7/10/2012 17:00:00 GARCIA, ALVIN LOGOUT 7/10/2012 17:00:00 AURENO, LEAH LOGOUT 7/10/2012 17:00:00 SALITA, LYNYRD ANTONIO LOGOUT 7/10/2012 17:00:00 CANSINO, PAUL LOGOUT 7/10/2012 17:00:00 BELO, RIO LOGOUT 7/10/2012 17:00:00 MAG-ISA, MAYBELLE LOGOUT 7/10/2012 17:00:00 TARINE, KAREN LOGOUT 7/10/2012 17:00:00 REYES, ANDREA LOGOUT 7/10/2012 17:00:00 NAVARRO, KRISTINA LOGOUT 7/10/2012 10:30:00 MAG-ISA, MAYBELLE LOGIN 7/10/2012 8:00:00 SALITA, LYNYRD ANTONIO LOGIN 7/10/2012 8:00:00 CANSINO, PAUL LOGIN 7/10/2012 8:00:00 BELO, RIO LOGIN 7/10/2012 7:40:00 AURENO, LEAH LOGIN 7/10/2012 7:30:00 GARCIA, ALVIN LOGIN 7/10/2012 7:25:00 TARINE, KAREN LOGIN 7/10/2012 7:10:00 NAVARRO, KRISTINA LOGIN 7/10/2012 7:10:00 REYES, ANDREA LOGIN
我希望将它作为查询处理,输出如下:
Log Date Employee Name LOGIN LOGOUT 9/13/2012 SALITA, LYNYRD ANTONIO 8:05:00 17:00:00 9/12/2012 SALITA, LYNYRD ANTONIO 8:05:00 17:05:00 7/10/2012 GARCIA, ALVIN 7:30:00 17:00:00 7/10/2012 AURENO, LEAH 7:40:00 17:00:00 7/10/2012 SALITA, LYNYRD ANTONIO 8:00:00 17:00:00 7/10/2012 CANSINO, PAUL 8:00:00 17:00:00 7/10/2012 BELO, RIO 8:00:00 17:00:00 7/10/2012 MAG-ISA, MAYBELLE 10:30:00 17:00:00 7/10/2012 TARINE, KAREN 7:25:00 17:00:00 7/10/2012 REYES, ANDREA 7:10:00 17:00:00 7/10/2012 NAVARRO, KRISTINA 7:10:00 17:00:00
我如何实现这一目标?
答案 0 :(得分:2)
这是你想要得到的:
SELECT DATE_FORMAT(LogDate, '%d/%c/%Y') AS LogDate, EmployeeName,
(GROUP_Concat(CASE LogType WHEN 'LOGIN' THEN LogTime END)) AS LOGIN,
(GROUP_Concat(CASE LogType WHEN 'LOGOUT' THEN LogTime END)) AS LOGOUT
FROM myTable
GROUP BY LogDate, EmployeeName
ORDER BY LogDate desc;
查询说明:
使用CASE
语句将行转换为列(Login
和Logout
)
SELECT DATE_FORMAT(LogDate, '%d/%c/%Y') AS LogDate, EmployeeName,
(CASE LogType WHEN 'LOGIN' THEN LogTime END) AS LOGIN,
(CASE LogType WHEN 'LOGOUT' THEN LogTime END) AS LOGOUT
FROM myTable
GROUP BY LogDate, EmployeeName, LogTime
ORDER BY LogDate desc;
但这会让你感觉像in this SQLFiddle.
因此,您需要使用GROUP_CONCAT
(MySQL)与LogTime
加入NULL
,并从LogTime
子句中删除GROUP BY
。
答案 1 :(得分:0)
Select Log Date, Employee Name, LOGIN LOGOUT
case(Log Type)
when 'LOGIN' then Log Time AS LOGIN
Else
Log Time AS LOGOUT
End
From
YOUR_TABLE
order by Log Date desc