Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write SUM(IF(boolean, integer, integer)) col_name in query in postgresql

I am new to Postgresql. I want to write a query like that:

SELECT SUM(IF(id in(1,2,3,4,5,6),1,0)) spl_count from tab group by id;

But it is not working. It is giving error like that:

function if(boolean, integer, integer) does not exist 

Actually I have tried same kind of query I mean sum(if(boolean, integer, integer)) in mysql. It is working there but not in postgresql. So how will I write that kind of query in postgresql?

like image 299
Joy Avatar asked Oct 19 '25 14:10

Joy


1 Answers

have you tried case?

SUM(CASE WHEN id IN (1, 2, 3, 4, 5, 6) THEN 1 ELSE 0 END)
like image 56
John Woo Avatar answered Oct 21 '25 07:10

John Woo