Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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,
like image 643
Sandro Motyl Avatar asked Oct 22 '25 14:10

Sandro Motyl


1 Answers

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

select (sum(if(condition, 1, 0)) as ?conditionCount) ...
like image 96
Joshua Taylor Avatar answered Oct 26 '25 06:10

Joshua Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!