从SPARQL中的COUNT获取MAX结果

时间:2018-06-04 23:42:37

标签: sparql

我有这个问题:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX curricula: <http://www.semanticweb.org/lsarni/ontologies/curricula#>
SELECT ?topic (COUNT(?x) as ?number_of_courses)
WHERE {
   ?topic curricula:taughtIn ?x .
}
GROUP BY ?topic
ORDER BY desc(?number_of_courses)

返回:

enter image description here

现在我希望获得?topic最多?number_of_courses,在这种情况下Engineering

我设法通过此查询得到了最大?number_of_coursesthis answer之后):

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX curricula: <http://www.semanticweb.org/lsarni/ontologies/curricula#>
SELECT (MAX(?number_of_courses) AS ?maxc)
WHERE{
  {
    SELECT ?topic (COUNT(?x) as ?number_of_courses)
    WHERE {
      ?topic curricula:taughtIn ?x .
    }
    GROUP BY ?topic
  }
}

返回:

enter image description here

但我无法获得?topic

我该如何解决这个问题?

更新

我尝试了从AKSW评论中理解的内容:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX curricula: <http://www.semanticweb.org/lsarni/ontologies/curricula#>
SELECT ?topic 
WHERE { 
  ?topic curricula:taughtIn ?x . 
  { 
    SELECT (MAX(?number_of_courses) AS ?max) 
    WHERE { 
      { 
        SELECT ?topic (COUNT(?x) as ?number_of_courses) 
        WHERE { 
           ?topic curricula:taughtIn ?x . 
         } 
         GROUP BY ?topic 
      }
    } 
  }
} 
GROUP BY ?topic
HAVING (COUNT(?x) = ?max)

但它什么也没有回报。

实施例

对于this minimal example,结果应为Topic_2,因为它是taughtIn两个课程。

@prefix : <http://www.semanticweb.org/lsarni/ontologies/curricula#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/lsarni/ontologies/curricula> .

<http://www.semanticweb.org/lsarni/ontologies/curricula> rdf:type owl:Ontology .

#################################################################
#    Object Properties
#################################################################

###  http://www.semanticweb.org/lsarni/ontologies/curricula#taughtIn
:taughtIn rdf:type owl:ObjectProperty ;
          rdfs:subPropertyOf owl:topObjectProperty .


###  http://www.w3.org/2002/07/owl#topObjectProperty
owl:topObjectProperty rdfs:domain :Topic ;
                      rdfs:range :Course .


#################################################################
#    Classes
#################################################################

###  http://www.semanticweb.org/lsarni/ontologies/curricula#Course
:Course rdf:type owl:Class ;
        owl:disjointWith :Topic .


###  http://www.semanticweb.org/lsarni/ontologies/curricula#Topic
:Topic rdf:type owl:Class .


#################################################################
#    Individuals
#################################################################

###  http://www.semanticweb.org/lsarni/ontologies/curricula#Course_1
:Course_1 rdf:type owl:NamedIndividual ,
                   :Course .


###  http://www.semanticweb.org/lsarni/ontologies/curricula#Course_2
:Course_2 rdf:type owl:NamedIndividual ,
                   :Course .


###  http://www.semanticweb.org/lsarni/ontologies/curricula#Topic_1
:Topic_1 rdf:type owl:NamedIndividual ,
                  :Topic ;
         :taughtIn :Course_1 .


###  http://www.semanticweb.org/lsarni/ontologies/curricula#Topic_2
:Topic_2 rdf:type owl:NamedIndividual ,
                  :Topic ;
         :taughtIn :Course_1 ,
                   :Course_2 .


###  Generated by the OWL API (version 4.2.8.20170104-2310) https://github.com/owlcs/owlapi

0 个答案:

没有答案