• Heres another way, but it assumes that you dont care if other Rows are duplicated, and will add rows to New_Table Provided the total in New_Table doesnt exceed 3 rows

    With Cte AS (

    Select

    ID

    ,National_code

    , ROW_NUMBER() OVER (Partition by National_Code Order by Id) Rn

    From #OLD_TABLE

    )

    INSERT INTO #New_Table

    ([ID],National_Code)

    SELECT [ID],c.[National_Code]

    FROM Cte c

    left JOIN (Select National_Code

    ,Count(*) e_cnt

    From #New_Table

    Group by National_Code) chk

    on chk.National_Code=c.National_Code

    Where Rn+ISnull(e_cnt,0)<=3

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices