In your version you have explicitly set the group for ALL TempId = 1 to 0, not just the first group. In a similar way you are ranking ALL TempId = 2 which is why your query gives the following results
rid TempId Sequence
1 1 0
2 1 0
3 1 0
4 1 0
5 2 1
6 2 2
7 1 0
8 1 0
9 2 1
10 2 2
11 2 3
12 2 4
13 2 5
14 1 0
15 2 1
16 2 2
17 2 3
18 2 4
19 2 5
20 2 6
21 2 7
22 2 8
You are not so much interested in the rank of row as you need the row that came before - i.e. you can use the LAG function. E.g.
SELECT Tempid, rid
,LAG(tempid,1, tempid) OVER (ORDER BY rid) prev
FROM zz_aud_ankit
Which gives these results
Tempid rid prev
1 1 1
1 2 1
1 3 1
1 4 1
2 5 1
2 6 2
1 7 2
1 8 1
2 9 1
2 10 2
2 11 2
2 12 2
2 13 2
1 14 2
2 15 1
2 16 2
2 17 2
2 18 2
2 19 2
2 20 2
2 21 2
2 22 2
Note the row where Rid = 5 and 6 though, more work is required to get the proper grouping. By using a simple "toggle" to check if TempId = Prev and summing that toggle we can get the proper grouping ...
;WITH cte1 AS
(
SELECT Tempid, rid
,LAG(tempid,1, tempid) OVER (ORDER BY rid) prev
FROM zz_aud_ankit
)
SELECT Tempid, Rid, prev
,sum(CASE WHEN tempid = prev THEN 0 ELSE 1 END)
OVER (ORDER BY rid ) AS groupid
FROM cte1
Which gives results
Tempid Rid prev groupid
1 1 1 0
1 2 1 0
1 3 1 0
1 4 1 0
2 5 1 1
2 6 2 1
1 7 2 2
1 8 1 2
2 9 1 3
2 10 2 3
2 11 2 3
2 12 2 3
2 13 2 3
1 14 2 4
2 15 1 5
2 16 2 5
2 17 2 5
2 18 2 5
2 19 2 5
2 20 2 5
2 21 2 5
2 22 2 5
That is also not quite your expected results but should be enough to point you in the right direction