0

I have three tables, one is tasks which are tasks that factory workers complete by putting them into batches, which is another table, each task has a batch_id, multiple tasks can have the same batch_id. The third table is the batch_log where all time that someone has worked on a batch is recorded in the form of a unique id, a batch_id, the person's userid, the start_time and the end_time.

Each task has an estimated_recurring time in seconds and an estimated_nonrecurring time in seconds. I'm looking to get the sum of both of those fields grouped by the operation_id which is a field in the tasks table to represent whether it is a laser cutting, deburr, painting, etc task.

The issue is that I only want the tasks that have batches that were in the batch_log during a time_period. Batches have multiple entries in the batch_log table.

Here is what I'm trying:

SELECT 
  operation_id, SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed
FROM tasks t
INNER JOIN batch_log l on t.batch_id = l.batch_id
WHERE 
  l.start_time BETWEEN DATE("10:00:00") AND DATE(NOW())
AND
  l.time_elapsed < "10:00:00"
GROUP BY t.operation_id

I'm concerned that I will get a higher estimate than is real because multiple entries in the task table can have the same batch.

davidahines
  • 3,976
  • 16
  • 53
  • 87
  • 1
    More info needed, what is multiple tasks, send us some sample data. What is up with the questions today? TGIF – JonH Sep 23 '11 at 13:29
  • Thanks Jon, for responding, I have updated the description, what kind of data should I post? – davidahines Sep 23 '11 at 13:37
  • 1
    your solution goes directly to the tasks table without considering the batches in the batch table. You need to join tasks->batches->batch log. See my solution below. – JonH Sep 23 '11 at 14:00

1 Answers1

1

Because you only want the times based on tasks that were in your batch log file what you posted will definately not work.

Assuming you have 3 tables: Tasks, Batches, and BatchLog.

You'll want this:

SELECT
      SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed,
      t.OperationID
FROM
      Tasks t
INNER JOIN
       Batches b
ON
       b.BatchID = t.BatchID
INNER JOIN
       BatchLog bl
ON
       bl.BatchID = b.BatchID
WHERE  
  bl.start_time BETWEEN DATE("2011-08-01") AND DATE(NOW())
GROUP BY
       t.OperationID
davidahines
  • 3,976
  • 16
  • 53
  • 87
JonH
  • 32,732
  • 12
  • 87
  • 145
  • This is really helpful. If I'm starting on the tasks table, shouldn't I join b.id on t.batch_id = b.id? Or is there a difference. EDIT: Apparently there is none, just checked. – davidahines Sep 23 '11 at 14:16
  • @dah - You own the tables not me :), you own the business logic not me :), I am only basing my answer on what little I know on your structure. I am reading it as 3 tables: tasks, batches, and batch_logs. Tasks can belong to the same batch. That is a one to many relationship between tasks->batches. You get that join to ensure you have proper tasks that at least have a batch. But then you got to step it one layer further to get only batches that were logged (inner join batches-> batch_log). And your comment states `shoundt I join b.id on t.batch_id = b.id`...remember I dont have the – JonH Sep 23 '11 at 14:20
  • ...continued: table structures so I am using columns that I made up. What I did to figure this out is use pencil and paper and put some sample data and create arrows to make the relationship. If an arrow exists you need some sort of join. If you want tasks that MUST have a batch with a batch log you know you'll need some sort of inner join (no left join as if you use left join you'd get a task without a batch log). – JonH Sep 23 '11 at 14:21
  • Thanks so much Jon, the results I get from this query are much, much better. – davidahines Sep 23 '11 at 14:22
  • So it's the batch table that keeps the answers sane because you don't end up with a result for every log, instead a result for every batch? – davidahines Sep 23 '11 at 14:43
  • @dah - exactly, you were missing that key join. – JonH Sep 23 '11 at 14:49