【问题标题】:MarkLogic: Using aggregate functions in SPARQL queriesMarkLogic:在 SPARQL 查询中使用聚合函数
【发布时间】:2017-12-23 14:51:25
【问题描述】:

我在marklogic学习资料中发现了以下查询:

PREFIX mo: <http://mlu.marklogic.com/ontology/> 
SELECT ?country ?max 
FROM <http://mlu.marklogic.com/populations> 
WHERE { 
        { 
          SELECT (MAX(?pop) as ?max) 
          WHERE {?country mo:population ?pop} 
        } 
        { 
          ?country mo:population ?pop . ?country mo:population ?max 
        }
}

我将其替换为:

select ?country ?max
from <http://mlu.marklogic.com/populations>
where{
    {
      select (MAX(?pop) as ?max)
      where {?country mo:population ?pop}
    }
    {?country mo:population ?max}
  }

似乎两个查询都返回了相同的结果。那么第一个查询中的 and 语句是否重要?我错过了什么吗?

【问题讨论】:

    标签: sparql marklogic semantics


    【解决方案1】:

    这实际上是一个奇怪的例子,区别并不明显,但有一个。首先让我们获取一些样本数据,我们将包括一个具有多个人口值的国家/地区。

    样本数据

    @prefix : <urn:ex:>
    
    :a :pop 200 .
    :b :pop 300 .
    :c :pop 400 .
    :d :pop 500 . # d has two population values
    :d :pop 600 .
    :e :pop 400 .
    :f :pop 600 . # f also has a maximum population
    

    您的查询

    现在,通过您的查询,您将返回恰好具有最大人口的国家/地区作为其值。其中可能不止一种。您正在匹配三元组 ?country :population MAX_POPULATION,并且对于每个 ?country 和 MAX_POPULATION,该三元组要么存在,要么不存在。 RDF 没有“重复的三元组”或类似的东西,所以每个 ?country 要么在里面,要么在外面。

    prefix : <urn:ex:>
    
    select ?c ?max {
      { select (max(?pop) as ?max) { ?c :pop ?pop } }
      { ?c :pop ?max }
    }
    
    ------------
    | c  | max |
    ============
    | :f | 600 |
    | :d | 600 |
    ------------
    

    他们的查询

    现在,通过他们的查询,您仍然会得到每个人口最多的国家/地区,但还有一个额外的变量在起作用。尽管 ?country :population MAX_POPULATION 只有一种匹配方式,但 ?country :population ?population 可能有更多方式。

    prefix : <urn:ex:>
    
    select ?c ?max {
      { select (max(?pop) as ?max) { ?c :pop ?pop } }
      { ?c :pop ?max . ?c :pop ?pop }
    }
    
    ------------
    | c  | max |
    ============
    | :f | 600 |
    | :d | 600 |
    | :d | 600 |
    ------------
    

    【讨论】:

    • 这是唯一且完美的答案!结论:示例查询很奇怪,应该更改为具有单个三元组模式的查询。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2019-12-03
    相关资源
    最近更新 更多