• untested but should be what you are after...

    with cteDeviceMovements

    as

    (

    SELECT dm.deviceId,

    m.movementId,

    m.date,

    Row_number() over (partition by dm.deviceId order by m.movementId)%2 as Rown

    FROM dbo.DeviceMovement dm

    INNER JOIN dbo.Movement m

    ON dm.movementId = m.movementId

    WHERE m.sourceLocationId = 99

    )

    Select *

    from cteDeviceMovements cross apply

    (Select top(1) * from cteDeviceMovements InnerDM

    where InnerDM.DeviceId = cteDeviceMovements.DeviceId

    and InnerDM.movementId > cteDeviceMovements.movementId) as NextDM

    where Rown = 1

    If you cant get it to work , please post full DDL and data in the form of INSERT's , thanks



    Clear Sky SQL
    My Blog[/url]