Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:19:25+00:00 2026-05-26T15:19:25+00:00

I’m running a query to find out how much estimated work was done on

  • 0

I’m running a query to find out how much estimated work was done on a factory floor and how much time was actually tracked in comparison to the amount of hours that station has available.

I”m doing this to determine which machines we need to purchase more of. Anything that we have a usage factor of over 100% is something that we’re over capacity.

The issue is that I’m getting astronomically high numbers for some operations. It is impossible that 5 men working each at a machine could track more than 120 hours however the result I am getting is well over a thousand.

What I do in the query is take all the batches, which have tasks and sum all of the estimated time of each tasks. I sum all of the time_elapsed in the batch_log and I multiply the hours_open by the number of machines of that operation.

Because of this, deburr should have a max of 120 hours as they are open 24 hours a day and there are 5 deburring stations. Does anything glaring jump out when looking at this query?

Please let me know if you need more info.

SELECT 
  DATE(bl.start_time) as date_tracked,
  o.name as operation,
  SUM(TIME_TO_SEC(bl.time_elapsed)/ 3600)  as time_elapsed,
  SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600  as estimated,
  o.hours_open as hours_open,  
    (count(distinct m.id)) as machine_count,
  hours_open * (count(distinct m.id)) as total_hours,
  (sum(TIME_TO_SEC(bl.time_elapsed)) / 3600) / (count(distinct m.id)) as time_elapsed_usage
FROM
  batches b
INNER JOIN 
  tasks t on b.id = t.batch_id
INNER JOIN  
  batch_log bl on b.id = bl.batch_id
INNER JOIN
  operations o on b.operation_id = o.id 
INNER JOIN
  machines m  on b.operation_id = m.operation_id
WHERE 
  bl.time_elapsed < "8:00:00"

GROUP BY
  b.operation_id,
  DATE(bl.start_time)
ORDER BY date_tracked, o.id

So I’ve started again and once I get to this point I seem to have duplication in the time elapsed:

select 
  batches.operation_id,
  date(batch_log.start_time) as date,
  SEC_TO_TIME(SUM(TIME_TO_SEC(batch_log.time_elapsed))) as elapsed,
    sum(tasks.estimated_nonrecurring + tasks.estimated_recurring) as estimated_time

from
  batches
INNER JOIN batch_log on batches.id = batch_log.batch_id
INNER JOIN tasks on batches.id = tasks.batch_id
WHERE batches.id not in (
-1,
-2,
-3,
-4,
-5,
-6,
-7,
-8,
-9,
-10,
-11,
-12,
-13,
-14
)
group by Date(batch_log.start_time), operation_id 
order by batch_log.start_time, batches.operation_id

EDIT: What am I doing wrong in the above? If I knew this I could be careful to structure queries better. Honestly, I haven’t been able to find anything and I’ve been digging through SQL books. Even if I could get an answer on the smaller statement I could make some progress. Working on other stuff for now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:19:25+00:00Added an answer on May 26, 2026 at 3:19 pm

    Clarifications please…

    Obviously Batch_Log multiple records per batch.
    Batch table, distinct batch ID.

    Now, on to tasks, operations and machines. 
    Can a batch have multiple tasks? 
    Can a batch have multiple operations? 
    Is the importance of distinct machines per operation critical?
    

    That said, here’s my review of your situation…
    First, I’m are getting only the batch logs time elapsed less than 8:00:00
    per your query. With that aggregation pre-grouped into single qualified
    batches, I can then do simple join to batches and tasks by those batch IDs.
    I can SUM() from tasks without worrying about double-counting as the starting
    basis is a single batch ID. Group all this by a batch ID simplifies the
    NEXT level joining to the Operations AND machines table

    Then, for the ones that are of aggregations, I have pre-aggregated
    those so they will return a single record respectively and reduce
    the possibility of Cartesian COUNT() and SUM() issues.

    WITH respect to machines. You have machines associated with an
    operation, but you are then grouping by operation and date. That
    being said, and it appears an operation CAN (and does) cross dates,
    one machine would be accounted for each day. Will that cause some
    possible skewed numbers??? Not sure, haven’t thought that far through.

    SELECT STRAIGHT_JOIN
          SmryByBatch.Operation_ID,
          SmryByBatch.Date_Tracked,
          SUM( SmryByBatch.Time_Elapsed ) Time_ElapsedByOpDate,
          SUM( SmryByBatch.Time_Elapsed ) / OpMachines.Machine_Count Time_ElapsedPerMachine,
          SUM( SmryByBatch.TaskEstByBatch ) TaskEstByOpDate,
          o.Name Operation,
          o.hours_open,
          OpMachines.Machine_Count,
          o.Hours_Open * OpMachines.Machine_Count as Total_Hours
       FROM 
           ( SELECT  
                b.Operation_ID,
                PreQuery.Batch_ID,
                PreQuery.Date_Tracked,
                PreQuery.TotalTimeElapsed / 3600 as Time_Elapsed,
                SUM( t.estimated_nonrecurring 
                   + t.estimated_recurring ) / 3600 as TaskEstByBatch
             FROM 
                ( SELECT
                        bl.batch_id,
                        DATE( bl.Start_Time ) date_tracked,
                        SUM( bl.time_elapsed ) TotalTimeElapsed
                     FROM
                        batch_log bl
                     WHERE
                        bl.time_elapsed < "8:00:00"
                     GROUP BY
                        bl.batch_ID,
                        DATE( bl.Start_Time ) ) PreQuery
    
                JOIN batches b
                   ON PreQuery.Batch_ID = b.ID
    
                JOIN Tasks t
                   ON PreQuery.Batch_ID = t.Batch_ID
    
            GROUP BY
               b.Operation_ID,
               PreQuery.Batch_ID ) SmryByBatch
    
          JOIN Operations o
             ON SmryByBatch.Operation_ID = o.ID
    
             JOIN ( select m.Operation_ID,
                           COUNT(distinct m.id)  machine_count
                       from
                          machines m
                       group by
                          m.Operation_ID ) OpMachines
                ON o.ID = OpMachines.Operation_ID
    
       GROUP BY 
          SmryByBatch.Date_Tracked
          SmryByBatch.Operation_ID,
    
       ORDER BY
          SmryByBatch.Date_Tracked,
          SmryByBatch.Operation_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.