Sandro Motyl
Sandro Motyl

Reputation: 139

How to do a COUNT with conditions (IF or FILTER) in SPARQL

I need to do a count only if some conditions be true in SPARQL, something like this (doesn't work):

SELECT ...
COUNT(FILTER(?qualisScoreValue = "A1")) ?QNTD_A1 .
COUNT(FILTER(?qualisScoreValue = "A2")) ?QNTD_A2 .
COUNT(FILTER(?qualisScoreValue = "B1")) ?QNTD_B1 .

I know mysql and I think that what I need should be equivalent in cases in mysql language like this (that works):

select ...
count(case when q.nameQualis = 'A1' then 1 else null end) as qntd_A1,
count(case when q.nameQualis = 'A2' then 1 else null end) as qntd_A2,
count(case when q.nameQualis = 'B1' then 1 else null end) as qntd_B1,

Upvotes: 4

Views: 1538

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85913

Rather than count, use sum and the if function for this. E.g.,

select (sum(if(condition, 1, 0)) as ?conditionCount) ...

Upvotes: 6

Related Questions