简体   繁体   中英

How can I write a query which returns the maximum value in SPARQL?

I want to have a query in SPARQL to find states and the university in each state with most undergraduate students.

I tried this:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?University ?State (MAX(?nu) AS ?size)
    WHERE{
        ?University dbpo:numberOfUndergraduateStudents ?nu.
        ?University a dbpo:University.
        ?University dbpo:state ?State.
        {
        SELECT ?State
            WHERE{
                ?State a dbpo:PopulatedPlace.
                ?State a dbpo:AdministrativeRegion.
                ?State dbpo:country dbpedia:United_States.
                ?State dbpo:capital ?Capital.
                FILTER(bound(?Capital))
            }
        }
    }

However, it does not return my expected output. it returns more than one school from one state. Can anybody guide me ?

You don't need the inner SELECT (it can be placed in the outer block).

FILTER(bound(?Capital))

-- ?Capital must be bound.

Then you need

GROUP BY ?State

Put this in an inner SELECT and use the outer one to find the actually univerity(-ies) with that size,state.

Your query is technically illegal currently:

SELECT ?University ?State (MAX(?nu) AS ?size)

with no GROUP BY is wrong. You can only select GROUP BY variables and aggregates

As AndyS says: the order in which to do things here is

  1. Find the maximum for the number of undergraduate students per state,
  2. Find the university or universities that have that number of undergraduate students.

The first step goes in the subquery, the second in the outer query. Then you get this:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?University ?State ?size
WHERE{
    ?University dbpo:numberOfUndergraduateStudents ?size.
    ?University a dbpo:University.
    ?University dbpo:state ?State.
    {
    SELECT ?State (MAX(?nu) as ?size)
        WHERE{
            ?State a dbpo:PopulatedPlace.
            ?State a dbpo:AdministrativeRegion.
            ?State dbpo:country dbpedia:United_States.
            ?State dbpo:capital ?Capital.
            ?University dbpo:numberOfUndergraduateStudents ?nu.
            ?University a dbpo:University.
            ?University dbpo:state ?State.
            FILTER(bound(?Capital))
        }
        GROUP BY ?State
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM