SPARQL selecting MAX value of a counter

2020-04-22 01:18发布

问题:

I'm new to sparql and trying to fegure out what is the best way to select the max value of a counter of another query without creating a new table, only using sub-queries. I'm working on a relatively small dataset so the computation time is not a problem.

SELECT ?p1 ?c1  (MAX(?cnt1) AS ?maxc)
WHERE{
{
SELECT ?p1 ?c1 (COUNT(?s1) AS ?cnt1)
WHERE {
    ?s1 ?p1 ?o1;
          a ?c1.
     }Group by ?p1 ?c1
#ORDER BY ?p1 DESC(?cnt1)
}

}GROUP BY ?p1

so I'm expecting to get a row for every value of ?p1 with the max ?cnt1 the suitable ?c1

I'm pretty sure that is the way to do that but for some reason it causes my endpoint to crash. the inner query works fine and when grouping by both ?p1 and ?c1 it produces just one line and the max value is empty.

Thanks, Omri

回答1:

Your query will crash unless you are grouping by both ?p1 and ?c1. When grouping, all variables appearing in SELECT must ether appear in the GROUP or in an aggregation function (MAX, COUNT, etc.).

The following query will give you the maximum value of your counter, but without the corresponding ?p1 ?c1. To have those, you will likely need another sub-query with a FILTER in it...

SELECT (MAX(?cnt1) AS ?maxc)
WHERE{
{
SELECT ?p1 ?c1 (COUNT(?s1) AS ?cnt1)
WHERE {
    ?s1 ?p1 ?o1;
          a ?c1.
     }Group by ?p1 ?c1
}
}