# mes.spGetWorkOrderDataSummary

## Description
This stored procedure summarizes key production and performance metrics for a specific asset and work order within a defined date range. It calculates availability, performance, and quality KPIs using production and counter data joined with work order and item information.

## Syntax
<b>mes.spGetWorkOrderDataSummary @assetId, @rangeStart, @rangeEnd, @workOrderId</b><br /><br />

<b>Parameters:</b><br />
@assetId (int): The asset ID for which data is retrieved.<br />
@rangeStart (datetime): The start of the reporting window.<br />
@rangeEnd (datetime): The end of the reporting window.<br />
@workOrderId (int): The specific work order to summarize.<br /><br />

<b>Returns:</b><br />
A row per work order and item with the following fields:<br />
- **targetCount**: Expected production count based on runtime and rate<br />
- **infeedCount / outfeedCount / wasteCount**: Raw and derived production totals<br />
- **runtimeSecs / unplannedDowntimeSecs / plannedDowntimeSecs / durationSecs**: Time-based metrics<br />
- **tgtOEE / tgtA / tgtP / tgtQ / tgtThreshold**: Work order KPI targets<br />
- **availability**: runtimeSecs / (runtimeSecs + unplannedDowntimeSecs)<br />
- **performance**: infeedCount / targetCount<br />
- **quality**: (infeedCount - wasteCount) / infeedCount<br />

### Code Example

```sql
EXEC mes.spGetWorkOrderDataSummary
  @assetId = 304,
  @rangeStart = '2025-07-01',
  @rangeEnd = '2025-07-30',
  @workOrderId = 12345;
