You could redesign your table to also have a counter:
ID product_id consecutive
8 bat 1
9 bat 2
10 abc 1
11 cat 1
12 dog 1
13 dog 2
When inserting to the table, first check the last entry and its consecutive counter. If you are inserting the same element, increase the counter, otherwise insert 1. Something like:
INSERT INTO Table (product_id, consecutive) VALUES
('newProd', CASE (SELECT product_id FROM Table WHERE ID = MAX(ID))
WHEN 'newProd' THEN
(SELECT consecutive FROM Table WHERE ID = MAX(ID)) + 1
ELSE 1
END);
Then you can make your selection as:
SELECT product_id, consecutive
FROM Table
WHERE ID IN
(SELECT MAX(ID)
FROM Table
GROUP BY product_id)
Original answer was:
SELECT Filename, COUNT(Filename) as Count
FROM Table
GROUP BY Filename