Querying Reason Selection
Reason selections are stored directly in the table and are not tied to the reason tree. This allows for the reason trees to be changed without affecting the history of the reason selections.
SELECT PU_Id,
Start_Time,
End_Time,
r1.Event_Reason_Name,
r2.Event_Reason_Name,
r3.Event_Reason_Name,
r4.Event_Reason_Name
FROM Timed_Event_Details ted
LEFT JOIN Event_Reasons r1 ON ted.Reason_Level1 = r1.Event_Reason_Id
LEFT JOIN Event_Reasons r2 ON ted.Reason_Level2 = r2.Event_Reason_Id
LEFT JOIN Event_Reasons r3 ON ted.Reason_Level3 = r3.Event_Reason_Id
LEFT JOIN Event_Reasons r4 ON ted.Reason_Level4 = r4.Event_Reason_Id
WHERE PU_Id = @PUId
AND Start_Time < @ReportEndTime
AND ( End_Time > @ReportStartTime
OR End_Time IS NULL)