WITH cte AS ( SELECT [ITEMID] ,[PURCHPRICE] ,[RETAILAMDISCOUNTTYPE] ,[CURRENCYCODE] ,[PURCHDISCPERCENT] ,[SALESPRICE] ,[VENDACCOUNT] ,[FROMDATE] ,[TODATE] ,[RETAILPRICECHANGESTATUS] ,[GROUPID] , row_number() OVER(PARTITION BY [ITEMID] ,[PURCHPRICE] ,[RETAILAMDISCOUNTTYPE] ,[CURRENCYCODE] ,[PURCHDISCPERCENT] ,[SALESPRICE] ,[VENDACCOUNT] ,[FROMDATE] ,[TODATE] ,[RETAILPRICECHANGESTATUS] ,[GROUPID] order by [ITEMID] ) AS [rn] FROM [ RETAILPRICECHANGETABLE ] ...