Calculate Pieces Produced per Hour in a Database Query
Question details
Calculate an hourly production rate from total pieces produced and elapsed operating hours in a database query.

- Product
- Database query (SQL)
- Device & OS
- not provided
- Scenario
- A production table records the total number of pieces and the elapsed running time in hours.
- Observed behavior
- The query should return pieces produced per hour while avoiding division by zero or null hours.
Identify the production table and the fields containing the total pieces and elapsed time. Confirm that elapsed time is stored as a numeric number of hours; convert it to hours first if it uses another unit or a time value.
Calculate the hourly production rate with a SQL query
Use this query to divide total production by running hours and return null for missing or nonpositive hours.
Open your database client’s SQL query editor and select the database containing the production table.
Enter: SELECT CASE WHEN Difference IS NULL OR Difference <= 0 THEN NULL ELSE 1.0 * TotalProduced / Difference END AS PiecesPerHour FROM SomeTable; Replace SomeTable, TotalProduced, and Difference with your actual table and field names.
Run the query using your database client’s Execute or Run button. For example, 626 pieces over 5 hours should return 125.2 pieces per hour.

Calculate pieces per hour in WPS Spreadsheets
Use a formula in WPS Spreadsheets to calculate the same hourly rate from exported production data. This spreadsheet method does not run a database query.
- 1. Open your production data: In WPS Spreadsheets, open a worksheet or exported file containing total pieces and elapsed hours.
- 2. Arrange the columns: Place total pieces in column A and numeric elapsed hours in column B, with headers in row 1.
- 3. Enter the formula: In cell C2, enter =IF(OR(NOT(ISNUMBER(A2)),NOT(ISNUMBER(B2)),B2<=0),"",A2/B2) and press Enter.
- 4. Apply it to other rows: Drag the fill handle from C2 down column C to calculate the rate for the remaining records.

Frequently Asked Questions
What is the hourly rate for 626 pieces produced in 5 hours?
Divide 626 by 5 to get 125.2 pieces per hour. In a spreadsheet, enter =626/5 in a cell to check the result.
How can I prevent division by zero in the SQL query?
Use a CASE expression to return NULL when the hours field is null or less than or equal to zero, and divide only in the ELSE branch.
Why does my query return a whole number instead of a decimal?
Some databases perform integer division when both fields are integers. Multiply the production value by 1.0 or cast it to a decimal type before dividing; the exact cast syntax depends on your database.
What if running time is stored as a spreadsheet time value?
Spreadsheet time values represent fractions of a day. If B2 contains a duration, multiply it by 24 to convert it to hours before dividing, for example =IF(B2>0,A2/(B2*24),"").
Can WPS Spreadsheets run the SQL query?
The spreadsheet formula calculates the rate from data in cells; it does not execute the SQL query. Run the query in your database client, or export the data and calculate the rate in WPS Spreadsheets.




