Amedeo Pachera
Amedeo Pachera

Reputation: 11

Compare result of 2 COUNT in SPARQL

How can I compare the results of 2 COUNT in the same SPARQL query? I want to know which number is bigger.

SELECT (COUNT(?x) as ?xCount, COUNT(?y) ad ?yCount)
WHERE
{?x rdf:type ex:A;
 ?y rdf:type ex:B;
}

Upvotes: 0

Views: 136

Answers (1)

AndyS
AndyS

Reputation: 16630

Firstly, the query will need to count each pattern separately:

SELECT *
WHERE
{ 
    SELECT (count(*) AS ?countA) { ?x rdf:type ex:A }
    SELECT (count(*) AS ?countB) { ?y rdf:type ex:B }
}

which puts the values into ?countA and ?countB.

To result which one is larger, compare the values (there are many ways):

BIND ( IF ( ?countA > ?countB , "countA larger" , "countB larger" ) AS ?result )

and together

SELECT ?result
WHERE
{ 
  { SELECT (count(*) AS ?countA) { ?x rdf:type ex:A } }
  { SELECT (count(*) AS ?countB) { ?y rdf:type ex:B } }
  BIND ( IF ( ?countA > ?countB , "countA larger" , "countB larger" ) AS ?result )
}

Upvotes: 1

Related Questions