Developer ToolsFree generatorPDF, Excel, Word exportDiscrete Manufacturing

Out-of-Tolerance Dimensional Inspection SQL Query Generator

Most quality systems store inspection values along with lower and upper limits. These three queries turn that raw data into a rejection list, a per-characteristic summary and an early warning for characteristics drifting toward a limit.

Fill in

Generated code
Download:

1. List every out-of-tolerance measurement

SELECT
    lot_no,
    characteristic_id,
    measured_value,
    lower_limit,
    upper_limit,
    CASE
        WHEN measured_value < lower_limit THEN measured_value - lower_limit
        ELSE measured_value - upper_limit
    END AS deviation_from_limit,
    inspected_at
FROM dbo.inspection_results
WHERE part_no = 'PN-48213'
  AND inspected_at >= '2026-07-01'
  AND inspected_at < '2026-09-30'
  AND (measured_value < lower_limit OR measured_value > upper_limit)
ORDER BY inspected_at DESC;

2. Summary per characteristic

SELECT
    characteristic_id,
    COUNT(*) AS measurements,
    SUM(CASE WHEN measured_value < lower_limit OR measured_value > upper_limit THEN 1 ELSE 0 END) AS out_of_tolerance,
    100.0 * SUM(CASE WHEN measured_value < lower_limit OR measured_value > upper_limit THEN 1 ELSE 0 END) / COUNT(*) AS pct_out,
    AVG(measured_value) AS mean_value,
    MIN(measured_value) AS min_value,
    MAX(measured_value) AS max_value,
    STDEV(measured_value) AS std_dev,
    MIN(lower_limit) AS lsl,
    MIN(upper_limit) AS usl
FROM dbo.inspection_results
WHERE part_no = 'PN-48213'
  AND inspected_at >= '2026-07-01'
  AND inspected_at < '2026-09-30'
GROUP BY characteristic_id
ORDER BY pct_out DESC, characteristic_id;

3. Characteristics close to a limit (mean within 25 percent of the band)

SELECT characteristic_id, AVG(measured_value) AS mean_value, MIN(lower_limit) AS lsl, MIN(upper_limit) AS usl
FROM dbo.inspection_results
WHERE part_no = 'PN-48213'
  AND inspected_at >= '2026-07-01'
  AND inspected_at < '2026-09-30'
GROUP BY characteristic_id
HAVING AVG(measured_value) < MIN(lower_limit) + 0.25 * (MIN(upper_limit) - MIN(lower_limit))
    OR AVG(measured_value) > MIN(upper_limit) - 0.25 * (MIN(upper_limit) - MIN(lower_limit));

Table and column names are generic placeholders, so map them to your own quality or ERP inspection tables. The script uses SQL Server syntax: use STDDEV or STDDEV_SAMP on Oracle, PostgreSQL and MySQL, and a bound parameter instead of an inline part number in production code. Limits that vary by lot should be grouped per limit pair. Test against a copy of the data before scheduling.

Ask your inspection data in plain English

Optional: a short review of how a private, read-only assistant could answer quality questions from your ERP and CMM data.

Optional. The tool and every export work without this. No spam.

How to use the generator

Enter the name of your inspection results table and map each column: part number, characteristic, lot, measured value, limits and date. Add the part number and the date range, then copy the queries into your SQL client or report tool. The first query lists every failing measurement with its distance from the nearest limit. The second summarizes each characteristic with count, percent out, mean, spread and limits. The third finds characteristics whose average sits close to a limit.

  • Run query 2 first to find which characteristics deserve attention
  • Use query 1 to pull the lots for containment or an NCR
  • Schedule query 3 weekly as a drift check

Why the summary matters

A percent-out figure only tells you that something failed. Comparing the mean and spread to the limits tells you why. A mean near one limit with a small standard deviation points to a centering problem, which is cheaper to fix with an offset. A mean at the center with a large standard deviation points to a variation problem, which needs a process fix. From the mean and the standard deviation you can also estimate Cpk as the smaller of (USL minus mean) and (mean minus LSL), divided by 3 sigma.

Reading the output

Suppose a bore characteristic shows 400 measurements, 12 out of tolerance, a mean of 25.012 against limits of 24.990 and 25.020, and a standard deviation of 0.006. The mean is within 0.008 of the upper limit, and the rejects are all above it. Shifting the process mean down by about 0.008 would remove most of the failures. That is the kind of conclusion a raw failure list does not give you.

ERP and data notes

Inspection data may live in an ERP quality module, a CMM database, an MES, or spreadsheets. Check how your system stores measurements: some store attribute results only, some store one row per sample with the limits in a separate plan table, requiring a join. Limits can change with drawing revision, so join to the revision if you need an exact historical picture. Confirm decimal precision and unit of measure before comparing values.

Frequently Asked Questions

Will this work with SAP QM or SyteLine tables?

The table and column names here are generic placeholders. You need to map them to your own schema, and in many ERPs the limits live in a separate plan table that has to be joined. Check your vendor data model before running against production.

How do I get a Cpk from these results?

Take the mean and standard deviation from query 2, then compute the smaller of (USL minus mean) and (mean minus LSL), and divide by three times the standard deviation. Use it only if the data is from a stable process and is reasonably normal.

Why is the date filter written with less than the end date?

Using a half-open range avoids losing results from the last day when the column has a time part. If you want the end date included, set the To date to the following day.

Is it safe to run on a production database?

These are read-only SELECT queries, but a large unindexed table can still slow other users. Run them on a reporting copy or with a narrow date range, check the execution plan, and use parameters rather than pasted values in any scheduled job.

If inspection data sits in an ERP or MES that is hard to query, Netray can help set up a read-only, on-prem assistant that answers these questions and shows the SQL it ran.