Overdue PM Work Orders SQL Query Generator
Overdue preventive maintenance is the first number a maintenance manager asks for, and the report in the CMMS often does not quite match how the plant defines late. This generator gives you plain SQL you can adapt to your own schema, so you can list overdue PMs and measure compliance by month.
Fill in
Replace with your CMMS or ERP table name
SQL Server: open PM work orders that are overdue
SELECT wo.wo_number,
wo.asset_id,
wo.due_date,
DATEDIFF(day, wo.due_date, CAST(GETDATE() AS date)) AS days_overdue
FROM work_orders wo
WHERE wo.work_type = 'PM'
AND wo.completed_date IS NULL
AND wo.due_date < DATEADD(day, -0, CAST(GETDATE() AS date))
ORDER BY days_overdue DESC;PostgreSQL: open PM work orders that are overdue
SELECT wo.wo_number,
wo.asset_id,
wo.due_date,
(CURRENT_DATE - wo.due_date::date) AS days_overdue
FROM work_orders wo
WHERE wo.work_type = 'PM'
AND wo.completed_date IS NULL
AND wo.due_date::date < CURRENT_DATE - 0
ORDER BY days_overdue DESC;PostgreSQL: PM compliance by month (completed on or before due date)
SELECT date_trunc('month', wo.due_date) AS due_month,
COUNT(*) AS pms_due,
COUNT(*) FILTER (WHERE wo.completed_date IS NOT NULL AND wo.completed_date::date <= wo.due_date::date + 0) AS pms_on_time,
ROUND(100.0 * COUNT(*) FILTER (WHERE wo.completed_date IS NOT NULL AND wo.completed_date::date <= wo.due_date::date + 0) / NULLIF(COUNT(*), 0), 1) AS compliance_pct
FROM work_orders wo
WHERE wo.work_type = 'PM'
AND wo.due_date < CURRENT_DATE
GROUP BY 1
ORDER BY 1;Table and column names are generic placeholders: no particular CMMS schema is assumed. Map them to your system (for example, check whether a status column is better than a null completion date), test on a copy of the data, and run read-only. Some systems store due dates as numbers or in a separate scheduling table.
Maintenance reporting from your ERP
Optional: we can set up read-only maintenance dashboards over your ERP data on your own infrastructure.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the names of your work order table and the columns that hold the work order number, asset, due date, completion date and work type. Set the value that marks a PM (some systems use PM, others use a numeric code or a separate order type). Copy the version for your database. Run it against a reporting replica or a copy where possible, and review the first results against a report you already trust.
- SQL Server uses DATEDIFF and GETDATE
- PostgreSQL uses date subtraction and CURRENT_DATE
- The grace parameter allows a few days of tolerance
- Adjust the completion test if your system uses a status column
What the queries calculate
The first query returns open PM work orders whose due date is earlier than today minus the grace period, ordered from the most overdue. The compliance query counts PMs due in each month and how many were completed on or before the due date plus the grace period, then divides. This is the common definition of PM compliance. Many sites use a window such as plus or minus 10 percent of the interval, which needs the interval in the query.
Definitions matter more than SQL
Agree the definition before comparing sites. Does compliance count the scheduled date or the original date after a reschedule? Does a PM cancelled because the asset was decommissioned count as due? Is a work order released but not started overdue? Write the rules next to the query. If a planner can reschedule freely, compliance can look excellent while the real interval is stretching.
Common mistakes
Do not join the work order table to a multi-row table such as labour or materials without grouping: you will count the same PM several times. Watch time zones on timestamp columns, and make sure that cancelled and rejected orders are excluded. Finally, do not run heavy queries on the live transaction database during a shift.
Frequently Asked Questions
Will this work with my CMMS out of the box?
Not without mapping. The names are generic on purpose because every CMMS and ERP uses a different schema. Replace the table and column names, and check how your system stores completion and cancellation. Test against a known report before relying on it.
How is PM compliance usually defined?
The most common definition is PM work orders completed on or before the due date, divided by PM work orders that came due in the period. Some plants add a tolerance window. Whichever you use, write it down and apply it consistently across sites.
Can I use this for Oracle or MySQL?
The logic carries over, but the date functions differ. In Oracle you can subtract dates directly and use SYSDATE, and in MySQL you use DATEDIFF and CURDATE. Edit the date expressions and the FILTER clause, which is PostgreSQL syntax, to a CASE expression.
Why is my overdue count different from the CMMS dashboard?
Usually because the dashboard excludes certain statuses, uses a scheduled date instead of a due date, or applies a grace period. Compare the definitions first, then compare a small sample of work orders one by one.
If you would rather ask this question in plain language, ERPray can answer it from your ERP data read-only and show you the exact query it ran.
Related Tools
SAP PM Preventive Maintenance Compliance Calculator
Turn counts from your SAP PM order list into PM compliance, overdue rate, planned work share and labor variance.
SAPSAP PM Maintenance Backlog Weeks Calculator
Turn the open work hours from your SAP PM order list into backlog in weeks, net burn rate and weeks to clear. Includes a crew-size sweep.
Maintenance & ReliabilityMTBF and MTTR From a Failure Log Python Script Generator
Name the columns in your CSV export and get a pandas script that works out MTBF, MTTR, failure count and availability for each asset.
Maintenance & ReliabilityMaintenance Technician Staffing Calculator
Work out how many full-time maintenance technicians your workload needs, using annual hands-on hours, backlog reduction and wrench time.
More Maintenance & Reliability tools
See all- MTBF and MTTR Worksheet
- TPM Autonomous Maintenance CIL Standard Template
- TPM Autonomous Maintenance Standard Template
- TPM Pillars and Autonomous Maintenance 7 Steps Reference
- Preventive Maintenance Schedule Template
- Maintenance Work Order Template
- Inherent vs Operational Availability Calculator (MTBF, MTTR)
- Spare Parts Criticality Scoring Worksheet
- Maintenance Backlog Calculator (Weeks of Work and Crew Size)
Keep exploring
Free tools
- SAP PM Preventive Maintenance Compliance CalculatorTurn counts from your SAP PM order list into PM compliance, overdue rate, planned work share and labor variance.
- PM Compliance and Schedule Compliance CalculatorCalculate PM compliance, completion rate, overdue tasks, hours variance and the share of effort spent on preventive work.
- On-Prem CMMS Private AI Layer Blueprint GeneratorFill in your On-Prem CMMS environment and get a shareable architecture plan for a private, read-only AI layer: components, controls, rollout and open questions.
ERP fixes and how-tos
- How to add a private AI layer to an on-prem CMMSHow to add a private AI assistant to an on-prem CMMS: read-only data access, work order retrieval, local models, permissions, approval-based write-back.
- SAP PP, PM and MM API: Production Orders, Maintenance Orders and Material MasterWhich SAP APIs cover production orders, maintenance orders and material master (MM02)? OData service names, BAPI alternatives and a sample GET call.
- How to add a private AI layer to Infor EAM on-premReference architecture to add AI to Infor EAM on-prem: read-only data path, retrieval, permissions, local GPUs, write-back control and rollout.
Guides
- Preventive Maintenance and ERP Integration: Closing the Loop Between Uptime and PlanningPreventive maintenance ERP integration guide: CMMS-ERP data flows, meter-based PM triggers, MRO inventory, scheduling impact, and AI-driven predictive maintenance.
- SyteLine SQL Server Maintenance GuideSyteLine SQL Server maintenance guide covering backups and recovery model, index and statistics jobs, integrity checks, and purging history tables safely.
AI on your ERP
- AI for UpKeep CMMS: Turn Work Order History Into Answers Without Handing It to a Public ModelUse AI with UpKeep CMMS through its REST API: question work orders, assets, parts and PM schedules, and tie maintenance data to your ERP and manuals.
- AI for eMaint CMMS: Reliability Questions Answered From Your Own Maintenance HistoryAdd private AI to Fluke Reliability eMaint CMMS: question work orders, equipment, PM and parts, and join condition data and ERP, with no public model.
Software
- Limble CMMSLimble CMMS explained: work orders, preventive maintenance, asset and parts tracking, API and integrations, fit for plants, limits and alternatives.
- UpKeepUpKeep explained: a mobile-first cloud CMMS for work orders, preventive maintenance, assets and inventory, with API options, limits and alternatives.