# mes.spGetAssetProductionEvents

## Description
Retrieves production mode events for a given asset over a date range. Optionally filters by mode, work order, or current mode. Calculates production-related metrics like counts and OEE components when applicable.

## Syntax
<b>mes.spGetAssetProductionEvents @assetId, @rangeStart, @rangeEnd, @modeId, @workOrderId, @activeModeOnly, @strictDates</b>  
<br /><br />

<b>Parameters:</b><br />
@assetId (int): ID of the asset to retrieve data for.<br />
@rangeStart (datetime): Start date of the time range (nullable).<br />
@rangeEnd (datetime): End date of the time range (nullable).<br />
@modeId (int): Optional filter by mode ID.<br />
@workOrderId (int): Optional filter by work order ID.<br />
@activeModeOnly (bit): If 1, only retrieves currently active production mode.<br />
@strictDates (bit): If 1, trims event times to fall strictly within the given range.<br /><br />

<b>Returns:</b><br />
Table of production events, enriched with:<br />
- Shift-adjusted start and end times<br />
- Counts, durations, OEE breakdowns (availability, performance, quality)<br />
- Related item and work order metadata

### Code Example

```sql
-- Retrieve production events for asset 4021, filtering by work order
EXEC mes.spGetAssetProductionEvents 
     @assetId = 4021,
     @rangeStart = '2025-07-01',
     @rangeEnd = '2025-07-30',
     @modeId = NULL,
     @workOrderId = 8001,
     @activeModeOnly = 0,
     @strictDates = 1;
