SPARQL分组

时间:2015-09-02 05:12:53

标签: group-by sparql

在SPARQL中,我可以使用GROUP BY列出一个组吗?

现在我的查询返回:

?p       ?p2
----------------
abc      zza
abc      zba
abc      zdf
bcd      zbc
bcd      zef
bcd      zhr
bcd      zfe
cde      zop
cde      zzz

我使用的查询是:

PREFIX  bo:  <https://webfiles.uci.edu/jenniyk2/businessontology#>
PREFIX   v:  <http://www.w3.org/2006/vcard/ns#>  
SELECT DISTINCT ?p ?p2 
WHERE
  { 
    ?p   v:hasAddress  ?ad  . 
    ?p2  v:hasAddress  ?ad  . 
      FILTER( ?p != ?p2 )
  }

有什么方法可以让它返回类似:

?p       ?p2
---------------
abc      zza
         zba
         zdf
bcd      zbc
         zef
         zhr
         zfe
cde      zop
         zzz

?p
-------------------
abc zza zba zdf
bcd zbc zef zhr zfe
cde zop zzz

1 个答案:

答案 0 :(得分:2)

这样的事情可以解决问题:

PREFIX bo:<https://webfiles.uci.edu/jenniyk2/businessontology#>
PREFIX v: <http://www.w3.org/2006/vcard/ns#>  

SELECT DISTINCT (GROUP_CONCAT(?p2; SEPARATOR=" ") AS ?p)
WHERE { 
    ?p1 v:hasAddress ?ad. 
    ?p2 v:hasAddress ?ad. 
} GROUP BY ?p1