Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am creating a web application based on attendance system. I am using MySQL and PHP. I have a table for every employee that has took attendance and they keep adding when the employees takes attendance on the machine. I extracted the employees that has has attendance 2 times per day which are in and out, and I added them to a table with columns. Now I want to extract the attendances for the employees that has took attendance 1 per day or more than 2 per day which they are error and i want to add them to a table employee time1, time2 and time3.

I have a table as this design, it's an attendance system,
I need to split the time if the attendance count per day was not equal to 2, which are error logs.
id    pd     time        date
1     5      07:05       08/07/2014
2     4      18:02       07/07/2014
3     1      07:05       06/07/2014
4     1      07:06       06/07/2014
5     1      18:00       06/07/2014

I need to add them to a table in the database and to be split in that form with respect the pd and date.
id   pd     time1     time2       time3    ....     date
1    5      07:05                                   08/07/2014
2    4      18:02                                   07/07/2014
3    1      07:05     07:06       18:00             06/07/2014

sorry guys its my first post in my whole entire life , so forgive me if am doing something wrong or not complete
Posted

1 solution

On a real database this could have been solved using a rank and a pivot.
But since MySQL doesn't support neither, and also not CTEs (Aargh).
You have to use a construct like this:
SQL
SELECT  
        pd,
        date,
        max(case rank when 1 then time else null end) as time1,
        max(case rank when 2 then time else null end) as time2,
        max(case rank when 3 then time else null end) as time3
FROM
    (
    SELECT  
            pd,
            date,
            time,
            find_in_set
                (
                time,
                    (
                    select  group_concat(time order by time)
                    from    MyTable t2
                    where   t1.pd = t2.pd
                    and t1.date=t2.date
                    )
                ) as rank
    FROM    MyTable t1
    ) as sub
GROUP BY pd,date
Performance will be horrible if your sets are big.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900