• hey, thats MUCH easier! and more efficient 😀

    thanks

    my final code is as follows:

    select IndividualCode,RecNum,codeTagPosition,DateReceived from (

    select *

    , ROW_NUMBER() over (partition by codeTagPosition,RecNum order by DateReceived,codeTagPosition) as myRowNum

    from (

    select

    j.value('@id','integer') as codeTagPosition

    , j.value('(./text())[1]','char(1)') as IndividualCode

    , OriginalSchema.value('Form[1]/Header[1]/AssessmentNumber[1]','INT') as RecNum

    , xm.DateReceived

    from

    table1 xm

    cross apply xm.originalschema.nodes('Form/Codes/code') x(j)

    where

    xm.act = 1

    and xm.state = 'Y'

    and schema.value('Form[1]/Header[1]/ClientId[1]','INT') = @ClientId

    )t

    where

    not t.IndividualCode is null

    )t2

    where t2.myRowNum = 1

    i have put that code into a UDF and OUTER APPLYed it in another query.