29

I am trying to run the following sql statement.

SELECT
item.item_number, SUM(item.item_active)
FROM 
public.item
GROUP BY item.item_number;

I am returning the following error:

ERROR:  function sum(boolean) does not exist

I figure if I can use a function to change the item.item_active to an integer, then it can take the sum of the active records.

Gurwinder Singh
  • 37,207
  • 6
  • 50
  • 70
Daniel L. VanDenBosch
  • 1,806
  • 3
  • 26
  • 51

2 Answers2

50

Try boolean_col::int:

SELECT
item.item_number, SUM(item.item_active::int)
FROM 
public.item
GROUP BY item.item_number;
Gurwinder Singh
  • 37,207
  • 6
  • 50
  • 70
4

If that value you are getting is boolean then you can write case statement to count your active records.

SELECT
item.item_number, SUM(case when item.item_active then 1 else 0 end)
FROM 
public.item
GROUP BY item.item_number;
Rams
  • 2,109
  • 1
  • 11
  • 19