I am using a database with bands, their albums and the release date of the albums. I'm trying to get a table with one column for the number of albums released in the 1980s, and one column for the number of albums released in the 1990s.
I expected a return table like this:
| band | numalbum1980s | numalbum1990s |
|---|---|---|
| <iri/ABBA> | 12 | 1 |
| <iri/Chicago | 5 | 3 |
This is my current attempt:
SELECT ?band (COUNT(?album1980s) as ?numalbum1980s) (COUNT(?album1990s) as ?numalbum1990s)
WHERE {
?album1980s :artist ?band .
?album1980s :date ?1980sdate
BIND(YEAR(?1980sdate) as ?1980syear)
FILTER(?1980syear > 1970 && ?1980syear < 1990)
}
GROUP BY ?band
UNION
WHERE {
?album1990s :artist ?band .
?album1990s :date ?1990sdate
BIND(YEAR(?1990sdate) as ?1990syear)
FILTER(?1990syear > 1990 && ?1990syear < 2000)
}
GROUP BY ?band
Which yielded this:
| band | numalbum1980 | numalbum1990s |
|---|---|---|
| <iri/ABBA> | 12 | 0 |
| <iri/Chicago | 5 | 0 |
As you see, the query results lack anything from the second WHERE clause entirely. I get a correct count in the numalbum1980s col, but just zeros in the numalbums1990s col.
I also tried encapsulating both WHERE clauses in a single clause, but that query is malformed:
SELECT ?band (COUNT(?album1980s) as ?numalbum1980s) (COUNT(?album1990s) as ?numalbum1990s)
{
WHERE {
?album1980s :date ?1980sdate
BIND(YEAR(?1980sdate) as ?1980syear)
FILTER(?1980syear > 1970 && ?1980syear < 1990)
}
UNION
WHERE {
?album1990s :date ?1990sdate
BIND(YEAR(?1990sdate) as ?1990syear)
FILTER(?1990syear > 1990 && ?1990syear < 2000)
}
?album1980s :artist ?band .
?album1990s :artist ?band .
}
GROUP BY ?band
If you have any thoughts of solutions, I'd love to hear them. (I'm currently using the SPARQL interface in cloud.stardog.com, but I don't think the interface matters.)