;WITH res1 AS ( SELECT * FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY F2 ORDER BY F1) AS rn,F1,F2 FROM dbo.test ) a ), res2 AS ( SELECT * FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY F2 ORDER BY F1) AS rn,F1,F2 FROM dbo.test ) b ) ),DATEDIFF(ms,res…