簡體   English   中英

SPARQL在多個圖中計數三倍

[英]SPARQL counting triples in multiple graphs

我想在一個特定類別的多個圖中計算三元組並將其相加。 我設法在每張圖中計數該類的三元組,但沒有設法計算總數。

初始查詢:

PREFIX locah: <http://data.archiveshub.ac.uk/def/>
PREFIX bs: <http://localhost:3030/alod/bs>
PREFIX ne: <http://localhost:3030/alod/ne>

SELECT (count(?sBS) as ?BScount) (count(?sNE) AS ?NEcount) WHERE {
  {
    GRAPH bs: {
      ?sBS a locah:ArchivalResource
    }
  } UNION {
    GRAPH ne: {
      ?sNE a locah:ArchivalResource
    }
  }
}    

我的想法是也簡單地使用SUM()函數,因此SELECT將如下所示:

SELECT (count(?sBS) as ?BScount) (count(?sNE) AS ?NEcount) (SUM(?NEcount ?BScount) AS ?total )WHERE {

但這似乎不起作用。

還有一個相關的問題:為什么我需要UNION? 如果我在不使用UNION的情況下執行它,似乎會得出很高的三倍計數,這沒有多大意義,並且計數是兩倍。

您可以在我的SPARQL端點上嘗試它: http : //data.alod.ch/sparql/

在投影中使用聚合時,必須按某些變量的不同值對解決方案進行分區或分組。 如果您未指定group by子句,則分組是隱式的。 在這種情況下,您有(至少)兩個選擇。 一種是使用兩個子查詢,如下所示:

select ?acount ?bcount (?acount + ?bcount as ?totalCount) where {
  { select (count(*) as ?acount) where {
      graph :a { ... } }
  { select (count(*) as ?bcount) where {
      graph :b { ... } }
}

我認為這可能是最簡單,最不言自明的選擇。 如前所述,另一個選擇是使用聯合:

select (count(?a) as ?acount)
       (count(?b) as ?bcount)
       (?acount + ?bcount as ?totalCount)
where {
  { graph :a { ?a ... } }
  union
  { graph :b { ?b ... } }
}

之類的東西

select (count(?a) as ?acount)
       (count(?b) as ?bcount)
       (?acount + ?bcount as ?totalCount)
where {
  graph :a { ?a ... }
  graph :b { ?b ... }
}

不起作用的是,您最終獲得了?a和?b值的笛卡爾積 即,假設對於αa有兩個值,對於αb有三個值。 然后,您在表中最終得到六行:

a1, b1  
a1, b2 
a1, b3
a2, b1  
a2, b2 
a2, b3

這些行中的每行都是唯一的,因此,如果使用隱式group by ,則將有六個a和六個b,這並不是您真正想要的。 但是,您仍然可以使用distinct

select (count(distinct ?a) as ?acount)
       (count(distinct ?b) as ?bcount)
       (?acount + ?bcount as ?totalCount)
where {
  #-- ...
}

查詢類型

類型1:子查詢

SELECT ?BScount ?NEcount (?BScount + ?NEcount as ?totalCount)
WHERE {
  { select (count(*) as ?BScount) WHERE {
      GRAPH bs: { ?sBS a locah:ArchivalResource }
    } }
  { select (count(*) as ?NEcount) WHERE {
      GRAPH ne: { ?sNE a locah:ArchivalResource }
    } }
}

類型2:聯盟

SELECT (count(?sBS) as ?BScount)
       (count(?sNE) AS ?NEcount)
       (?BScount + ?NEcount as ?totalCount)
WHERE {
  { GRAPH bs: { ?sBS a locah:ArchivalResource } }
  UNION
  { GRAPH ne: { ?sNE a locah:ArchivalResource } }
}

類型3:笛卡爾積和不同

SELECT (count(distinct ?sBS) as ?BScount)
       (count(distinct ?sNE) AS ?NEcount)
       (?BScount + ?NEcount as ?totalCount)
WHERE {
  { GRAPH bs: { ?sBS a locah:ArchivalResource } }
  { GRAPH ne: { ?sNE a locah:ArchivalResource } }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM