hi!
When we want to use order by, with `LIMIT` and `OFFSET`, with datasets over 10000 rows, e.g., `LIMIT 100 OFFSET 14000` we get error:

> Virtuoso 22023 Error SR353: Sorted TOP clause specifies more then 14100 rows to sort. Only 10000 are allowed. Either decrease the offset and/or row count or use a scrollable cursor

In order to avoid this error we are using subqueries as suggested in [Working with SPARQL endpoint constraints via `LIMIT` & `OFFSET`](https://vos.openlinksw.com/owiki/wiki/VOS/VirtTipsAndTricksHowToHandleBandwidthLimitExceed)
 

However, there is a problem when using `MAX (in group)` function. We need `MAX` because there are some `sosa:ObservationCollection` with more than one `sosa:resultTime`/`sosa:phenomenonTime` and we just need to take one

For example, see queries below (you can test in https://www.foodie-cloud.org/sparql) 

- query 1 returns the first 100 rows correctly, but the `ORDER BY` is ignored. Rows are returned in random? order
- the query 2 returns rows in correct order, but the `LIMIT` and `OFFSET` are ignored

Is there a way to make both work properly, where all, `ORDER BY`, plus `LIMIT` and `OFFSET` work properly?

### Query1
```sparql
PREFIX sosa:  <http://www.w3.org/ns/sosa/>

SELECT DISTINCT ?id MAX(?phenomenonTime) as ?p MAX(?resultTime) as ?r

FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>

WHERE {
    SELECT DISTINCT ?id ?phenomenonTime ?resultTime
    WHERE {
        ?col    a                           sosa:ObservationCollection;
                sosa:resultTime             ?resultTime;
                sosa:phenomenonTime         ?phenomenonTime;
                sosa:hasMember              ?s.
        ?s      rdf:type/rdfs:subClassOf*   sosa:Observation;
                sosa:hasResult              ?result.

        BIND(REPLACE(STRAFTER(STR(?s), "observation/"), "/", "-") AS ?id)

    }
    ORDER BY ?id
} 
GROUP BY ?id
LIMIT 100 OFFSET 0
```

### Query2
```sparql
PREFIX sosa:  <http://www.w3.org/ns/sosa/>

SELECT DISTINCT ?id ?p ?r

FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>

WHERE {
    SELECT DISTINCT ?id MAX(?phenomenonTime) as ?p MAX(?resultTime) as ?r
    FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>
    WHERE {
        ?col    a                           sosa:ObservationCollection;
                sosa:resultTime             ?resultTime;
                sosa:phenomenonTime         ?phenomenonTime;
                sosa:hasMember              ?s.
        ?s      rdf:type/rdfs:subClassOf*   sosa:Observation;
                sosa:hasResult              ?result.

        BIND(REPLACE(STRAFTER(STR(?s), "observation/"), "/", "-") AS ?id)

    }
    GROUP BY ?id
    ORDER BY ?id
}
LIMIT 100 OFFSET 0
```
sorting with `LIMIT`, `OFFSET`, `MAX`, and subqueries
This is a GitHub Issue
repo:virtuoso-opensource
owner:openlink
Title : sorting with `LIMIT`, `OFFSET`, `MAX`, and subqueries
Issue date:

--- start body ---
hi!
When we want to use order by, with `LIMIT` and `OFFSET`, with datasets over 10000 rows, e.g., `LIMIT 100 OFFSET 14000` we get error:

> Virtuoso 22023 Error SR353: Sorted TOP clause specifies more then 14100 rows to sort. Only 10000 are allowed. Either decrease the offset and/or row count or use a scrollable cursor

In order to avoid this error we are using subqueries as suggested in [Working with SPARQL endpoint constraints via `LIMIT` & `OFFSET`](https://vos.openlinksw.com/owiki/wiki/VOS/VirtTipsAndTricksHowToHandleBandwidthLimitExceed)
 

However, there is a problem when using `MAX (in group)` function. We need `MAX` because there are some `sosa:ObservationCollection` with more than one `sosa:resultTime`/`sosa:phenomenonTime` and we just need to take one

For example, see queries below (you can test in https://www.foodie-cloud.org/sparql) 

- query 1 returns the first 100 rows correctly, but the `ORDER BY` is ignored. Rows are returned in random? order
- the query 2 returns rows in correct order, but the `LIMIT` and `OFFSET` are ignored

Is there a way to make both work properly, where all, `ORDER BY`, plus `LIMIT` and `OFFSET` work properly?

### Query1
```sparql
PREFIX sosa:  <http://www.w3.org/ns/sosa/>

SELECT DISTINCT ?id MAX(?phenomenonTime) as ?p MAX(?resultTime) as ?r

FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>

WHERE {
    SELECT DISTINCT ?id ?phenomenonTime ?resultTime
    WHERE {
        ?col    a                           sosa:ObservationCollection;
                sosa:resultTime             ?resultTime;
                sosa:phenomenonTime         ?phenomenonTime;
                sosa:hasMember              ?s.
        ?s      rdf:type/rdfs:subClassOf*   sosa:Observation;
                sosa:hasResult              ?result.

        BIND(REPLACE(STRAFTER(STR(?s), "observation/"), "/", "-") AS ?id)

    }
    ORDER BY ?id
} 
GROUP BY ?id
LIMIT 100 OFFSET 0
```

### Query2
```sparql
PREFIX sosa:  <http://www.w3.org/ns/sosa/>

SELECT DISTINCT ?id ?p ?r

FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>

WHERE {
    SELECT DISTINCT ?id MAX(?phenomenonTime) as ?p MAX(?resultTime) as ?r
    FROM <http://w3id.org/iliad/dataaset/jf-pilot-sample>
    WHERE {
        ?col    a                           sosa:ObservationCollection;
                sosa:resultTime             ?resultTime;
                sosa:phenomenonTime         ?phenomenonTime;
                sosa:hasMember              ?s.
        ?s      rdf:type/rdfs:subClassOf*   sosa:Observation;
                sosa:hasResult              ?result.

        BIND(REPLACE(STRAFTER(STR(?s), "observation/"), "/", "-") AS ?id)

    }
    GROUP BY ?id
    ORDER BY ?id
}
LIMIT 100 OFFSET 0
```
--- end body ---
