SPARQL Example 4 – count, group by

COUNT 사용 1

PREFIX test: <http://test.com/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT COUNT(*) 
WHERE {
?id foaf:name ?name ;
test:birthYear ?birth .
FILTER(?birth>1960)
OPTIONAL { ?id test:deathYear ?death . }
}

COUNT 사용 2

PREFIX test: <http://test.com/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT COUNT( DISTINCT ?birth )
WHERE {
?id foaf:name ?name ;
test:birthYear ?birth .
FILTER(?birth>1960)
OPTIONAL { ?id test:deathYear ?death . }
}

GROUP BY 사용

PREFIX test: <http://test.com/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?birth COUNT(?id)
WHERE {
?id foaf:name ?name ;
test:birthYear ?birth .
FILTER(?birth>1980)
}
GROUP BY ?birth

 

more …