--i have table with following structure.--SELECT SaleID, vender_inv, Code, cDate--FROM SaleMain--i want to fetch last two invoice against each code.--where saleid is auto incremented and cdate as well to get last --invoice.select *from(select SaleId, Vender_Inv, Code, cDate,ROW_NUMBER() over (partition by Code order by cDate Desc) as RowNumberFROM SaleMain ) Awhere A.RowNumber < 3