• Try this query, should give you the same result

    SELECT COUNT(a.dex_row_id)

    FROM (SELECT dex_row_id, ROW_NUMBER() OVER (PARTITION BY idfaktury ORDER BY dataexportu DESC) AS RowNo

    FROM akint10001) AS a

    WHERE RowNo > 1

    but as already mentioned we need much more to determine how to optimise the query