3

我在对每日时间记录中的数据进行分组和排序以自动生成报告时遇到问题。DTR的表结构如下:

LogDate LogTime EmployeeName LogType
2012-09-14 10:48:04 SALITA, LYNYRD ANTONIO 注销
2012-09-14 09:39:29 SALITA,LYNYRD ANTONIO 注销
2012-09-14 09:39:19 SALITA, LYNYRD ANTONIO 登录
2012-09-14 09:35:25 SALITA,LYNYRD ANTONIO 注销
2012-09-14 09:35:13 SALITA, LYNYRD ANTONIO 登录
2012-09-14 08:10:00 SALITA,LYNYRD ANTONIO 登录
2012-09-13 17:00:00 SALITA,LYNYRD ANTONIO 注销
2012-09-13 08:05:00 SALITA, LYNYRD ANTONIO 登录
2012-09-12 17:05:00 SALITA,LYNYRD ANTONIO 注销
2012-09-12 08:05:00 SALITA, LYNYRD ANTONIO 登录
2012-07-10 17:00:00 MAG-ISA,美宝莲登出
2012-07-10 17:00:00 BELO,里约热内卢注销
2012-07-10 17:00:00 卡西诺,保罗登出
2012-07-10 17:00:00 SALITA,LYNYRD ANTONIO 注销
2012-07-10 17:00:00 AURENO,莉亚登出
2012-07-10 17:00:00 加西亚,阿尔文登出
2012-07-10 17:00:00 TARINE,凯伦登出
2012-07-10 17:00:00 REYES,ANDREA 注销
2012-07-10 17:00:00 纳瓦罗,克里斯蒂娜 注销
2012-07-10 10:30:00 MAG-ISA,美宝莲登录
2012-07-10 08:00:00 SALITA,LYNYRD ANTONIO 登录
2012-07-10 08:00:00 卡西诺,保罗登录
2012-07-10 08:00:00 贝洛,里约登录
2012-07-10 07:40:00 AURENO,莉亚登录
2012-07-10 07:30:00 加西亚,阿尔文登录
2012-07-10 07:25:00 TARINE,凯伦登录
2012-07-10 07:10:00 纳瓦罗,克里斯蒂娜登录
2012-07-10 07:10:00 REYES,ANDREA 登录

使用这个 sql:

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;

我能够产生这个结果

LogDate EmployeeName 登录 注销
2012-09-14 SALITA, LYNYRD ANTONIO 08:10:00,09:35:13,09:39:19 09:35:25,09:39:29,10:48:04
2012-09-13 SALITA, LYNYRD 安东尼奥 08:05:00 17:00:00
2012-09-12 SALITA, LYNYRD 安东尼奥 08:05:00 17:05:00
2012-07-10 雷耶斯,安德烈亚 07:10:00 17:00:00
2012-07-10 纳瓦罗,克里斯蒂娜 07:10:00 17:00:00
2012-07-10 塔林,凯伦 07:25:00 17:00:00
2012-07-10 加西亚,阿尔文 07:30:00 17:00:00
2012-07-10 奥雷诺,莉亚 07:40:00 17:00:00
2012-07-10 卡西诺,保罗 08:00:00 17:00:00
2012-07-10 萨利塔,林尼德安东尼奥 08:00:00 17:00:00
2012-07-10 贝洛,里约热内卢 08:00:00 17:00:00
2012-07-10 MAG-ISA, MAYBELLE 10:30:00 17:00:00

根据其中一个答案,这是代码:

SELECT DATE_FORMAT(t1.LogDate, '%d/%c/%Y') AS LogDate, t1.EmployeeName
     , t1.LogTime AS Login
     , ( SELECT MIN(t2.LogTime) FROM myTable t2
          WHERE t2.LogType = 'LOGOUT'
            AND t2.LogDate = t1.LogDate
            AND t2.EmployeeName = t1.EmployeeName
            AND t2.LogTime > t1.LogTime ) AS Logout
  FROM myTable t1
 WHERE t1.LogType = 'LOGIN'

它产生了这个结果:

LogDate EmployeeName 登录 注销
2012-09-14 SALITA, LYNYRD 安东尼奥 08:10:00 09:35:25
2012-09-14 萨利塔,林尼德安东尼奥 09:35:13 09:35:25
2012-09-14 SALITA, LYNYRD ANTONIO 09:39:19 09:39:29
2012-09-13 SALITA, LYNYRD 安东尼奥 08:05:00 17:00:00
2012-09-12 SALITA, LYNYRD 安东尼奥 08:05:00 17:05:00
2012-07-10 雷耶斯,安德烈亚 07:10:00 17:00:00
2012-07-10 纳瓦罗,克里斯蒂娜 07:10:00 17:00:00
2012-07-10 塔林,凯伦 07:25:00 17:00:00
2012-07-10 加西亚,阿尔文 07:30:00 17:00:00
2012-07-10 奥雷诺,莉亚 07:40:00 17:00:00
2012-07-10 卡西诺,保罗 08:00:00 17:00:00
2012-07-10 萨利塔,林尼德安东尼奥 08:00:00 17:00:00
2012-07-10 贝洛,里约热内卢 08:00:00 17:00:00
2012-07-10 MAG-ISA, MAYBELLE 10:30:00 17:00:00

有没有办法让结果像这样排序?

LogDate EmployeeName 登录 注销
2012-09-14 SALITA,LYNYRD ANTONIO 08:10:00 NULL
2012-09-14 萨利塔,林尼德安东尼奥 09:35:13 09:35:25
2012-09-14 SALITA, LYNYRD ANTONIO 09:39:19 09:39:29
2012-09-14 SALITA,LYNYRD ANTONIO NULL 10:48:04
2012-09-13 SALITA, LYNYRD 安东尼奥 08:05:00 17:00:00
2012-09-12 SALITA, LYNYRD 安东尼奥 08:05:00 17:05:00
2012-07-10 雷耶斯,安德烈亚 07:10:00 17:00:00
2012-07-10 纳瓦罗,克里斯蒂娜 07:10:00 17:00:00
2012-07-10 塔林,凯伦 07:25:00 17:00:00
2012-07-10 加西亚,阿尔文 07:30:00 17:00:00
2012-07-10 奥雷诺,莉亚 07:40:00 17:00:00
2012-07-10 卡西诺,保罗 08:00:00 17:00:00
2012-07-10 萨利塔,林尼德安东尼奥 08:00:00 17:00:00
2012-07-10 贝洛,里约热内卢 08:00:00 17:00:00
2012-07-10 MAG-ISA, MAYBELLE 10:30:00 17:00:00
4

1 回答 1

4

像这样的东西:

SELECT DATE_FORMAT(t1.LogDate, '%d/%c/%Y') AS LogDate, t1.EmployeeName
     , t1.LogTime AS Login
     , ( SELECT MIN(t2.LogTime) FROM myTable t2
          WHERE t2.LogType = 'LOGOUT'
            AND t2.LogDate = t1.LogDate
            AND t2.EmployeeName = t1.EmployeeName
            AND t2.LogTime > t1.LogTime ) AS Logout
  FROM myTable t1
 WHERE t1.LogType = 'LOGIN'

没有 GROUP BY 真的是必要的,因为你实际上并没有对任何东西进行分组。

于 2012-09-14T02:32:14.973 回答