4

我正在尝试编写一个 SPARQL 查询来计算多个图中对象的出现次数。示例数据和预期输出如下:

命名图 g1:

@prefix g1: <http://example.com/g1#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
g1:ex1 rdfs:label "a" .
g1:ex2 rdfs:label "a" .
g1:ex3 rdfs:label "b" .
g1:ex3 rdfs:label "d" .

命名图g2:

@prefix g2: <http://example.com/g2#> .    
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
g2:ex1 rdfs:label "a" .
g2:ex2 rdfs:label "b" .
g2:ex3 rdfs:label "c" .

SPARQL 查询的预期输出:

?label ?g1count ?g2count
a      2        1
b      1        1
c      0        1
d      1        0

我可以通过合并 rdfs:labels 并计算出现次数来获得两个图的总数:

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?label count(?label) as ?count
{
  {
    GRAPH g1: {
      ?s rdfs:label ?label
    }
  } UNION {
    GRAPH g2:
    {
      ?s rdfs:label ?label
    }
  }
}

我认为从这里我可以使用每个 UNION 块中的子查询来获取单个计数,但是除了这种查询可能效率低下之外,我没有任何运气获得预期的结果。

4

2 回答 2

4

从 RobV 的回答中打高尔夫球(评论太大):

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?label (count(?s1) as ?g1count) (count(?s2) AS ?g2count)
{
  {
    GRAPH g1: {
      ?s1 rdfs:label ?label
    }
  } UNION {
    GRAPH g2: {
      ?s2 rdfs:label ?label
    }
  }
} group by ?label order by ?label

结果:

---------------------
| label | g1c | g2c |
=====================
| "a"   | 2   | 1   |
| "b"   | 1   | 1   |
| "c"   | 0   | 1   |
| "d"   | 1   | 0   |
---------------------
于 2013-01-22T10:38:35.603 回答
2

您可以利用COUNT函数忽略未绑定值并为变量指定不同名称的事实,即

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT (COALESCE(?label, ?label2) AS ?label) (count(?label1) as ?g1count) (count(?label2) AS ?g2count)
{
  {
    GRAPH g1: {
      ?s rdfs:label ?label1
    }
  } UNION {
    GRAPH g2:
    {
      ?s rdfs:label ?label2
    }
  }
}

COALESCE函数用于将实际值组合到标签中成为单个变量,因为COALESCE从参数返回第一个非空值

于 2013-01-21T18:39:51.630 回答