繁体   English   中英

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

[英]How can I get a max values in a query sparql

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

#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. 如果需要每个顾问的Nobel类别总数,则应使用SUM。
  2. 如果您想要最高的人数,顾问可以通过他/她的任何学生获得...
  3. 如果你想 ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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