0

I'm having a problem in grouping and sorting data in our daily time record to automatically produce a report. The table structure of the DTR as follows:

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

I want it to be processed as a query with an output like this:

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

How do I achieve this?

1
  • 6
    Wich RDBMS: MS SQL Server, MySQL, Postgre, Oracle? And also, what have you tried? Post a working query, no matter its erroneous, maybe you are just one step close to the solution
    – Yaroslav
    Sep 13, 2012 at 7:57

2 Answers 2

2

This is what you are trying to get:

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;

See this SQLFiddle

Description of the query:

Use CASE Statement to convert row to column (for Login and 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;

But this will result you like in this SQLFiddle.

So you need to use GROUP_CONCAT (MySQL) to join LogTime with NULL and remove LogTime from GROUP BY clause.

4
  • 1
    thnx this solved the problem. I just need to calibrate the variables! thnx again! Sep 13, 2012 at 8:54
  • is there a way to show only the first login and the last login? Sep 14, 2012 at 3:24
  • @LynyrdSalita Can't understand your sentence show only the first login and the last login. Can you show your desired output?
    – Himanshu
    Sep 14, 2012 at 4:02
  • ow, im sorry for the typo, that meant to be last logout Sep 14, 2012 at 6:45
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.