33

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
  • 38,557
  • 6
  • 51
  • 76
Daniel L. VanDenBosch
  • 2,350
  • 4
  • 35
  • 60

2 Answers2

60

Try boolean_col::int:

SELECT
item.item_number, SUM(item.item_active::int)
FROM 
public.item
GROUP BY item.item_number;
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
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,129
  • 1
  • 12
  • 19