簡體   English   中英

SPARQL中的Group_concat

[英]Group_concat in SPARQL

我是SPARQL的初學者,我正在嘗試處理西班牙國家圖書館的終結點。

我有一個有效的代碼,這里是:

prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/> 
select distinct 
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
?subjectLabel
where { 
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL {  ?book bne:P3013 ?ISBN }
OPTIONAL {  ?book bne:P3001 ?publisher }
OPTIONAL {  ?book bne:P3006 ?date }
OPTIONAL {  ?book bne:P3004 ?pags }
OPTIONAL {  ?book bne:P3007 ?size }
OPTIONAL {  ?book bne:P3016 ?series }
OPTIONAL {  ?book bne:P1004 ?date }
OPTIONAL {  ?book bne:P3017 ?edition }
OPTIONAL {  ?book bne:P3014 ?subtitle }
OPTIONAL {  ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50

但是由於某些書有兩個或兩個以上的主題,因此SPARQL在結果中會重復這些主題。 我使用了group_concat ,但是由於某種原因它不起作用:

prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/> 
select distinct 
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
(GROUP_CONCAT(DISTINCT(?subjectLabel); separator="//") as ?subjects)
where { 
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL {  ?book bne:P3013 ?ISBN }
OPTIONAL {  ?book bne:P3001 ?publisher }
OPTIONAL {  ?book bne:P3006 ?date }
OPTIONAL {  ?book bne:P3004 ?pags }
OPTIONAL {  ?book bne:P3007 ?size }
OPTIONAL {  ?book bne:P3016 ?series }
OPTIONAL {  ?book bne:P1004 ?date }
OPTIONAL {  ?book bne:P3017 ?edition }
OPTIONAL {  ?book bne:P3014 ?subtitle }
OPTIONAL {  ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
group by ?book
order by ?date

有人知道我在哪里犯錯嗎?

謝謝!

編輯:

我做錯了一件事情:正如@AKSW所說,我必須在代碼末尾對所有變量進行分組,或者在select上添加變量。 我有用於測試此代碼的簡化版本:

PREFIX  bne:  <http://datos.bne.es/def/>
PREFIX  resource: <http://datos.bne.es/resource/>

SELECT  ?book ?author ?title (GROUP_CONCAT(DISTINCT ?subject ; separator='//') AS ?subjects)
WHERE
  { ?book  a                     bne:C1003 ;
           bne:P3001             "Errata Naturae" ;
           bne:P1011             ?author ;
           bne:P3002             ?title ;
           bne:OP3008            ?subject
  }
#group everything here
GROUP BY ?book ?author ?title

@JeenBroekstra,當我在SPARQL驗證程序中運行它時,它說沒問題,但是當我嘗試在該庫的SPARQL端點中運行它時,出現一個錯誤:

Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'GROUP_CONCAT' before '('

如評論中所述,問題是目標端點截至2011-11-16的Virtuoso,Open Source Edition,v06.01.3127上運行,該端點不支持SPARQL中的GROUP_CONCAT ,因為SPARQL 1.1尚未完成。

強烈建議升級到當前版本

在此版本的Virtuoso中有一個內置函數,如記錄的那樣,可以通過sql:group_concat現在可以使用。

暫無
暫無

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

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