如何在查询 sparql 中获取最大值

How can I get a max values in a query sparql

我想知道在另一个 SELECT 中应用计数后如何在 sparql 查询中获取最大值。我的代码是:

#PREFIX nobel: <http://data.nobelprize.org/terms/>
#PREFIX cat: <http://data.nobelprize.org/resource/category/>
#PREFIX foaf: <http://xmlns.com/foaf/0.1/>
#PREFIX dbo: <http://dbpedia.org/ontology/> 
#PREFIX dbp: <http://dbpedia.org/property/>
#PREFIX dbr: <http://dbpedia.org/resource/>
#PREFIX owl: <http://www.w3.org/2002/07/owl#>
#PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
#PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

Select ?advisorName (max (?c) as ?m ) {#the answer is empty.. )
SELECT ?advisorName (count (distinct (?category)) as ?c) (max (?c) as ?m) { 
     ?student a nobel:Laureate ;
            owl:sameAs ?dbpStudent ;
            foaf:name ?studentName ;
            nobel:nobelPrize ?pStudent .          
     ?pStudent nobel:category ?category .   

FILTER (afn:namespace(?dbpStudent) = str(dbr:))        
SERVICE <http://dbpedia.org/sparql> {    
{ ?dbpStudent dbo:doctoralAdvisor ?dbpAdvisor .}
union
{?dbpAdvisor dbo:doctoralStudent ?dbpStudent.  }    
 ?dbpAdvisor rdfs:label ?advisorName  .
Filter (lang(?advisorName)= "en")   }}
group by ?dbpStudent ?advisorName
order by desc (?c)  }

非常感谢!

这不是答案,而是用作更复杂的评论!

在我们开始调试之前,您的查询应采用标准 SPARQL 语法:

SELECT  ?advisorName (MAX(?c) AS ?m)
WHERE
  { { SELECT  ?advisorName (COUNT(DISTINCT ?category) AS ?c)
      WHERE
        { ?student  rdf:type          nobel:Laureate ;
                    owl:sameAs        ?dbpStudent ;
                    foaf:name         ?studentName ;
                    nobel:nobelPrize  ?pStudent .
          ?pStudent  nobel:category   ?category
          FILTER ( afn:namespace(?dbpStudent) = str(dbr:) )
          SERVICE <http://dbpedia.org/sparql>
            {   { ?dbpStudent  dbo:doctoralAdvisor  ?dbpAdvisor }
              UNION
                { ?dbpAdvisor  dbo:doctoralStudent  ?dbpStudent }
              ?dbpAdvisor  rdfs:label  ?advisorName
              FILTER ( lang(?advisorName) = "en" )
            }
        }
      GROUP BY ?advisorName
    }
  }
GROUP BY ?advisorName
ORDER BY DESC(?c)

没看懂的地方:

  • 为什么你在外部 SELECT 查询的变量 ?dbpStudent 上有一个 group by 因为没有这样的绑定,因为它不是由内部 SELECT 查询返回的
  • 为什么你也在内部 SELECT 查询中计算 MAX

所以我的问题是,查询的总体目标是什么?

  1. 如果您想要每个顾问的诺贝尔类别总数,您应该使用 SUM。
  2. 如果您想要顾问可以通过任何 his/her 学生获得的最高数字,...
  3. 如果你想...

一旦我们确定了您的查询应该做什么,我们就可以通过检查中间结果开始调试。