2013-04-23 56 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