Getting Labels in SPARQL Aggregate

25 views Asked by At

The following query is working:

SELECT  ?goal (count(?ngo) as ?ngoCount)
WHERE {?ngo a ngo:NGORecipient;
            ngo:hasSDGGoal ?goal.
        ?goal rdfs:label ?sdglabel.}
            GROUP BY ?goal
            ORDER BY ?ngoCount

However, when I go to get the label rather than the IRI as follows

SELECT  ?sdglabel (count(?ngo) as ?ngoCount)
WHERE {?ngo a ngo:NGORecipient;
            ngo:hasSDGGoal ?goal.
        ?goal rdfs:label ?sdglabel.}
            GROUP BY ?goal
            ORDER BY ?ngoCount

I get an error: "Executing query failed: All selected variables must be aggregates but ?sdglabel is not."

1

There are 1 answers

1
Michael DeBellis On

Thanks! This works fine:

SELECT  ?sdglabel (count(?ngo) as ?ngoCount)
WHERE {?ngo a ngo:NGORecipient;
            ngo:hasSDGGoal ?goal.
        ?goal rdfs:label ?sdglabel.}
            GROUP BY ?sdglabel
            ORDER BY ?ngoCount

I don't have to worry about IRIs with the same label because there shouldn't be any. If I'm understanding it would fail if there were but just to be sure I did a query to test for any SDGs with different IRIs but the same label and as I expected there aren't any.