# mes.spGetOEEDataByIntervalGroupByShift

## Description
This stored procedure calculates and returns Overall Equipment Effectiveness (OEE) metrics for a given asset over a time range, grouped by shift. It computes runtime, downtime, counter-based performance, and derives availability, performance, quality, and OEE. It aggregates the data by time intervals (hour/day/week) and shifts.

## Syntax
<b>mes.spGetOEEDataByIntervalGroupByShift @assetId, @startDate, @endDate, @interval</b><br /><br />

<b>Parameters:</b><br />
@assetId (int): Asset identifier for which to compute OEE<br />
@startDate (datetime): Start of the date range<br />
@endDate (datetime): End of the date range<br />
@interval (varchar): Time interval for aggregation ('Hour', 'Day', or 'Week')<br /><br />

<b>Returns:</b><br />
One row per shift per interval with fields:<br />
- Shift ID and name<br />
- Time interval (hourTimestamp, duration)<br />
- Runtime and downtime (planned/unplanned)<br />
- Standard rate and package/item units<br />
- Infeed, outfeed, and waste counts<br />
- Target production and efficiency metrics:<br />
  - **Availability** = runtime / duration<br />
  - **Performance** = infeed / (standardRate × runtime)<br />
  - **Quality** = (infeed - waste) / infeed<br />
  - **OEE** = Availability × Performance × Quality<br />

### Code Example

```sql
EXEC mes.spGetOEEDataByIntervalGroupByShift
    @assetId = 1001,
    @startDate = '2025-07-01',
    @endDate = '2025-07-15',
    @interval = 'Day';
