每年仅在SPARQL中显示一张照片

时间:2019-11-25 22:41:50

标签: sql group-by sparql

我的问题很简单。但是我无法掌握如何使用SPARQL进行操作。

我有一个日期列表(格式YYYY-mm-dd)。对于每个日期,我都创建了一个Eventschema:PublicationEvent)。当PhotosEvent相等时,photo-date链接到该event-date。现在,我想列出每年一次并且每年只显示一张照片。在我目前的查询中,我每年获得多张照片。我想每年有一张照片。有人可以帮我吗?

PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX spif: <http://spinrdf.org/spif#>
select distinct ?newDate (count(?photoRef) as ?photoCount)
from <http://archief.nl/events/Graph:anpGebeurtenissen>

PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX spif: <http://spinrdf.org/spif#>
select distinct ?newDate (count(?photoRef) as ?photoCount)
from <http://archief.nl/events/Graph:anpGebeurtenissen>
where { 
    ?dateEventURI a schema:PublicationEvent;
               rdfs:label ?dateEventLabel;
               schema:image ?photoRef.

    bind(substr(?dateEventLabel, "1", "4") as ?newDate)

} 
group by ?newDate ?photoRef
order by ?newDate

结果是这样的。对于1937年,显示的照片有3个URI。但是,我每年希望看一张照片(uri)。

1   1937    http://test.nl/NL/doc/PhotoReference:A8BD26A2D0B4102DBCF8003048976D84
2   1937    http://test.nl/NL/doc/PhotoReference:AF33B3B6D0B4102DBCF8003048976D84
3   1937    http://test.nl/NL/doc/PhotoReference:AF33B906D0B4102DBCF8003048976D84
4   1938    http://test.nl/NL/doc/PhotoReference:AEC963E4D0B4102DBCF8003048976D84
5   1938    http://test.nl/NL/doc/PhotoReference:AEC964A2D0B4102DBCF8003048976D84

1 个答案:

答案 0 :(得分:1)

sample()完成了这项工作。谢谢!

查询如下。

# Deze query toont voor elke jaar de referentie naar één foto
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX spif: <http://spinrdf.org/spif#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select ?year (sample(?photoRef) as ?photo)
from <http://test.nl/test/Graph:anpGebeurtenissen>
where { 

  ?dateEventURI a schema:PublicationEvent;
            rdfs:label ?dateEventLabel;
            foaf:img ?photoRef.             
  bind(substr(?dateEventLabel, "1", "4") as ?year)
} 
group by ?year
order by asc(?year)

结果看起来像这样

1   1937    http://test.nl/doc/PhotoReference:AF33B906D0B4102DBCF8003048976D84
2   1938    http://test.nl/doc/PhotoReference:AEC9690CD0B4102DBCF8003048976D84
3   1939    http://test.nl/doc/PhotoReference:AF74502ED0B4102DBCF8003048976D84
4   1946    http://test.nl/doc/PhotoReference:A89E1398D0B4102DBCF8003048976D84
5   1947    http://test.nl/doc/PhotoReference:AD7A738ED0B4102DBCF8003048976D84
6   1948    http://test.nl/doc/PhotoReference:A8BA1246D0B4102DBCF8003048976D84
7   1949    http://test.nl/doc/PhotoReference:A8CC0FA0D0B4102DBCF8003048976D84
8   1950    http://test.nl/doc/PhotoReference:A8DF786AD0B4102DBCF8003048976D84
9   1951    http://test.nl/doc/PhotoReference:A8E5A7DAD0B4102DBCF8003048976D84