2013-04-23 57 views
1

我有一些利用新的Data Cube vocabulary的RDF数据。为了可视化,我需要将数据作为平坦而紧凑的表格。但是我无法找到正确的SPARQL查询。正在寻找一个创建紧凑表的SPARQL查询

我会尽量给一个小例子...

鉴于以下RDF数据:

o1 
:year "2007"; 
:fruit :apples; 
:harvest "200"; 
. 
o2 
:year "2007"; 
:fruit :pears; 
:harvest "180"; 
. 
o3 
:year "2008"; 
:fruit :apples; 
:harvest "205"; 
. 
o4 
:year "2008"; 
:fruit :pears; 
:harvest "176"; 
. 

是否有一个SPARQL查询,可以返回的数据如下表?

Year | Apples | Pears 
2007 | 200 | 180 
2008 | 205 | 176 

在此先感谢!

回答

3

尝试:

select ?year (sample(?num_apples) as ?apples) (sample(?num_pears) as ?pears) 
where 
{ 
    { ?s :year ?year ; :fruit :apples; :harvest ?num_apples } 
    union 
    { ?s :year ?year ; :fruit :pears; :harvest ?num_pears } 
} 
group by ?year 

你可以使用sum()avg()而非sample(),如果您有多个收获人物这会更有意义。

(警告:你的数值是字符串而不是数字!)

+0

是的,这有效!非常感谢!所以诀窍是使用聚合函数。事后看来,这很有道理。 是的,我知道我省略了数据类型规范。我这样做是为了保持简单的例子。但是这种方法适用于我的真实数据。我需要更改的唯一方法是在SAMPLE函数之前使用sql:前缀,因为我使用了Virtuoso端点。 – user952460 2013-04-23 15:29:38