在SPARQL中插入数据时可以使用聚合函数吗? [英] It is possible to use aggregate function when inserting data in SPARQL?

查看:100
本文介绍了在SPARQL中插入数据时可以使用聚合函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,旨在防止当三元组的数量为给定数(例如5)时,在命名图中插入.

I have the following query which aims to prevent the insertion in a named graph when the amount of triples is of a given number, e.g., 5.

我试图重现此处所示的示例数据是可能的,但我需要使用相同的命名图,最重要的是聚合函数,即COUNT.

I tried to reproduce the example showed here where the insertion of data is possible but I need to use the same named graph and, most important an aggregate function, i.e., COUNT.

INSERT 
  { 
    GRAPH <http://example/g1> { ?s ?p ?o } 
  }
WHERE
  { 
  GRAPH  <http://example/g1>
   { 
    ?s ?p ?o .
    FILTER ( COUNT(?p) < 5)
   } 
}  

Jena Fuseki引发以下错误:

Jena Fuseki raises the following error:

 Aggregate expression not legal at this point

推荐答案

是的,这是可能的:

INSERT 
  { 
    GRAPH <http://example/g2> { ?s ?p ?o } 
  }
WHERE
{ 
  GRAPH <http://example/g1> {
    ?s ?p ?o .           # select all triples
    { SELECT ?s          # do a check for the selected subject
      WHERE { GRAPH <http://example/g1> { ?s ?p ?o } }
      GROUP BY ?s            # count properties per subject
      HAVING (COUNT(?p) < 5) # only subjects with less than 5 properties 
    }
  }
}

首先,不能将聚合运算符放在FILTER子句中,要将它们用作条件,必须将它们放在HAVING子句中.

First of all, aggregate operators cannot be placed inside a FILTER clause, to use them as a conditional you have to put them in a HAVING clause instead.

此外,由于要对每个主题的属性进行计数,因此必须使用GROUP BY ?s并使用与简单模式匹配相结合的子选择(检查主题对属性数量的限制).

Furthermore since you want to count properties per subject, you have to use GROUP BY ?s and use a subselect (which checks the restriction on the number of properties for a subject) combined with a simple pattern match.

编辑请注意,我对您的原始更新进行了一些修改:它正在从您查询的图插入到不同的命名图中.插入相同名称的图形当然可以,但是结果将是不可观察的(因为您只是插入该图形中已经存在的相同数据).

EDIT Note that I slightly modified your original update: it's inserting into a different named graph from the one that you are querying. Inserting into the same named graph will of course work, but the results will not be observable (as you are just inserting the same data that was already present in that graph).

这篇关于在SPARQL中插入数据时可以使用聚合函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆