0

let me start off with the fact that i'm still kind of new to php/mysql.

I'm trying to build a system that can be used to record jobs completed for customers that keeps track of job numbers, customer name, equipment, dates, and employee's assigned to the task.

I have a table setup to handle the employee's assigned, but I can't seem to figure out how to format the results the way I would like.

There could be multiple employee's assigned to the same job over several days. So I setup a table like:

job_ID  |  employee_name  |  date_worked  |  hours_worked
  1     |      Dave       |   2010-1-2    |     4.5
  1     |      Dave       |   2010-1-3    |     4.0
  1     |      Mike       |   2010-1-2    |     6.0
  2     |      Bill       |   2011-5-7    |     8.0
  2     |      Bill       |   2011-5-8    |     6.5
  2     |      Dave       |   2011-5-7    |     4.0
  2     |      Dave       |   2011-5-8    |     4.5

What i'm looking to do with the results is output it like:

    Job # = 1
    Employee  |  2010-1-2  |  2010-1-3  | 
      Dave    |     4.5    |     4.0    |
      Mike    |     6.0    |      0     |

    Job # = 2
    Employee  |  2011-5-7  |  2011-5-8  | 
      Bill    |     8.0    |     6.5    |
      Dave    |     4.0    |     4.5    |

Can anyone offer advice?

EDIT

Ok, so I guess I left out some information.

I have a table (job_list) that is used to keep records.

This table records the job_id, client, equipment, location, call date, response date, completion date, etc.

Then I have a table (employee_list) that lists all employees by id, and name.

The idea behind the table I posted earlier was to be the table that just records who was where, on what day, and for how long.

I've amended the table i posted to have employee_id.

And I would like to display the results in php to look like the output i posted. I just don't know how to do it.

What other information can I provide? Apologies for my lack of understanding on what I need to post :-/

And if my approach is completely horrible, I did say I'm still new to this...

6
  • 2
    What have you tried? Commented Nov 15, 2012 at 21:20
  • "employee_name", really?, really? You should have a separate table that stores the employees details, hours_worked should REALLY have it's own table so you can set who/hours worked/job .. Do you want to display the result in PHP? Is that the question, I am confused!! You have set your tables up wrong. Commented Nov 15, 2012 at 21:22
  • You should restructure your layout(just an idea) table: jobs - job_id - job_name - job_description - ... table: employees - employee_id - employee_name - ... table: work - employee_id - job_id - date - hours worked Commented Nov 15, 2012 at 21:27
  • Restructure your tables, as the others said. Commented Nov 15, 2012 at 21:28
  • i've updated my post. fixed the employee_name to an id that references and employee_list table. Commented Nov 15, 2012 at 22:00

1 Answer 1

1

This is basically a PIVOT but MySQL does not have a PIVOT function so you will need to replicate it using an aggregate function and a CASE statement. If you know all of the values that you want to transform your query will be similar to this:

select employee_name,
  sum(case when date_worked = '2010-01-02' then hours_worked else 0 end) `2010-01-02`,
  sum(case when date_worked = '2010-01-03' then hours_worked else 0 end) `2010-01-03`
from yourtable
group by employee_name

See SQL Fiddle with Demo

The result is:

| EMPLOYEE_NAME | 2010-01-02 | 2010-01-03 |
-------------------------------------------
|          Bill |          0 |          0 |
|          Dave |        4.5 |          4 |
|          Mike |          6 |          0 |

If you have an unknown number of values, then you can use a prepared statement to generate this dynamically:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'sum(case when date_worked = ''',
      date_worked,
      ''' then hours_worked else 0 end) AS `',
      date_worked, '`'
    )
  ) INTO @sql
FROM yourtable;

SET @sql = CONCAT('SELECT employee_name, ', @sql, ' 
                  FROM yourtable 
                   GROUP BY employee_name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

See SQL Fiddle with Demo

Both will produce the same result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.