I have below question:
Want to find the consecutive duplicates
SLNO NAME PG
1 A1 NO
2 A2 YES
SELECT MAX(consecutives) -- Block 1
FROM (
SELECT t1.pg, t1.slno, COUNT(*) AS consecutives -- Block 2
FROM test t1 INNER JOIN test t2 ON t1.pg = t2.pg
WHERE t1.slno <= t2.slno
AND NOT EXISTS (
SELECT * -- Block 3
FROM test t3
WHERE t3.slno > t1.slno
AND t3.slno < t2.slno
AND t3.pg != t1.pg
)
GROUP BY t1.pg, t1.slno
);
The query calculates the result in following way:
PG
in between (blocks 2 and 3)PG
value and starting SLNO
value -> this counts the consecutive values for any [PG
, (starting) SLNO
] couple (block 2);Note that the query may be simplified if the slno field in table contains consecutive values, but this seems not your case (in your example record with SLNO
= 5 is missing)