Fix pagination on MSSQL2000 with complex queries

When the FROM uses joins on subqueries, the regexps where taking the first matching WHERE/GROUP/HAVING instead of the last one, which was creating an invalid query.

Also add different case when offset is zero, unnecessary to have complicated query, just use TOP.