开发者

MySQL to get the count of rows for each worker with specific dates and jobs

I have a database of workers and their assigned work items. I want to create a report using MySQL that will show a count of how many work items under certain jobs and within certain date ranges each worker has. The resulting data would look something like this:

           Today          Tomorrow        Next Year
Worker | Job1 Job2 Job3 | Job1 Job2 Job3 | Job1 Job2 Job3

Bob    |  4    0   1    | 1    2    0    | 5    10   3

I have one table with the following relevant fields: worker, date, jobtype, workitem

Each worker has multiple entries under the same name, so Bob would have multiple workitems assigned to him, like this:

Bob | 07/27/2011 | sandblasting | workitem001
Bob | 08/30/2011 | mowing | workitem001
Bob | 08/30/2011 | driving | workitem002

Workitems can be assigned to multiple jobtypes.

I wrote some 开发者_如何学运维confusing loops with multiple queries, but ideally I want to write a single, complex query to accomplish this. Is that even possible?


overview for each day:

select worker, date, job, count(*) as count
from workitems
group by worker, date, job

overview for month/year:

select worker, month(date) as month, job, count(*) as count
from workitems
group by worker, month, job

That's it. The table format is little different as in mysql you cannot put values as columns (job1, job2, job3 in your example), as it is possible with e.g. MS Access' cross query. But you can export the data from mysql with the above queries and then e.g. use MS Excel contingency tables to produce the output you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜