2017-08-03 103 views
2

我一直在研究SPARQL查询以在图形存储中查找唯一值组合。但我没有成功。SPARQL计数唯一值组合

基本上我尝试做的是:

a b c 
e f g 
e r t 
a b c 
k l m 
e f g 
a b c 


result: 
a b c | 3 
e f g | 2 
e r t | 1 
k l m | 1 

试过几种结构,与distincts,组by`s和子查询,但我不取得成功。

最后一次尝试:

SELECT (count (*) as ?n){ 
     SELECT DISTINCT ?value1 ?value2 ?value3 WHERE { 
     ?instance vocab:relate ?value1 . 
     ?instance vocab:relate ?value2 . 
     ?instance vocab:relate ?value3 . 
     } 
    } 

RDF:你需要一个排序标准添加到值,这样你不考虑所有不同的可能的方式:

<http://test.example.com/instance1> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> . 

<http://test.example.com/instance6> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> . 

<http://test.example.com/instance4> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> . 

<http://test.example.com/instance2> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> . 

<http://test.example.com/instance7> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> . 

<http://test.example.com/instance5> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/m> , <http://test.example.com/l> , <http://test.example.com/k> . 

<http://test.example.com/instance3> 
     a  <http://test.example.com#Instance> ; 
     <http://vocab.example.com/relate> 
       <http://test.example.com/t> , <http://test.example.com/r> , <http://test.example.com/e> . 
+1

我猜你只需要一个'FILTER(?值1 <值2 &&值2 <值3)',但我无法测试它,你没有告诉我们的RDF(样本)数据 – AKSW

+0

增加了RDF到最初的帖子 – user3599600

回答

4

AKSW's comment上是现货订购价值。此外,请记住RDF没有“重复”的三倍,所以

:a :p :c, :c, :d 

相同

:a :p :c, :d 

所以适当的比较是<而不是<=,因为没有重复的三倍,你'd从来没有=的情况。另外,由于这些值是IRI,因此您需要先获取它们的字符串值,然后才能与<进行比较,但str函数将处理此问题。

prefix v: <http://vocab.example.com/> 
prefix : <http://test.example.com/> 

select ?a ?b ?c (count(distinct ?i) as ?count) where { 
    ?i v:relate ?a, ?b, ?c . 
    filter (str(?a) < str(?b) && str(?b) < str(?c)) 
} 
group by ?a ?b ?c 
------------------------ 
| a | b | c | count | 
======================== 
| :a | :b | :c | 3  | 
| :e | :f | :g | 2  | 
| :e | :r | :t | 1  | 
| :k | :l | :m | 1  | 
------------------------ 
+0

感谢似乎工作。 – user3599600