user952460
user952460

Reputation: 133

Looking for a SPARQL query that creates a compact table

I have some RDF data that make use of the new Data Cube vocabulary. For visualization, I need the data as a flat and compact table. But I fail at finding the right SPARQL query that does this.

I will try to give a minimal example...

Given the following RDF data:

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";
. 

Is there a SPARQL query that can return the data as the following table?

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

Thanks in advance!

Upvotes: 1

Views: 156

Answers (1)

user205512
user205512

Reputation: 8898

Try:

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

You could use sum() or avg() rather than sample(), which would make more sense if you have multiple harvest figures.

(Warning: your values are strings rather than numbers!)

Upvotes: 3

Related Questions