SELECT
    Source.ID AS SourceID,
    PQ_Events.EventId AS EventID,
    Source.Name AS Source,
    PQ_Events.StartTimestampUtc,
    PQ_Events.V1Duration,
    PQ_Events.V1Magnitude,
    PQ_Events.V2Duration,
    PQ_Events.V2Magnitude,
    PQ_Events.V3Duration,
    PQ_Events.V3Magnitude,
    NominalVoltage.Value AS Nominal
FROM
    PQ_Events JOIN
    Source ON PQ_Events.SourceId = Source.ID JOIN
    SourceType ON Source.SourceTypeID = SourceType.ID JOIN
    Quantity NominalVoltageQuantity ON NominalVoltageQuantity.Name = 'Voltage Disturbance Detection Nominal Voltage' OUTER APPLY
    (
        SELECT TOP 1 *
        FROM DeviceSettings
        WHERE
            SourceID = Source.ID AND
            QuantityID = NominalVoltageQuantity.ID AND
            FirstLoggedUTC <= PQ_Events.DatalogTimestampUtc
        ORDER BY FirstLoggedUTC DESC
    ) NominalVoltage
WHERE
    PQ_Events.IsSagSwell <> 0 AND
    PQ_Events.V1Duration > 0 AND
    PQ_Events.V2Duration > 0 AND
    PQ_Events.V3Duration > 0 AND
    SourceType.Name = '8650'