Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: function avg(boolean) does not exist

Tags:

sql

postgresql

I am trying to know how frequently an event happens, and the occurence of this event in my database is recorded by setting a boolean to 'TRUE' and the non-occurence is setting it to 'FALSE'.

But when I am try to select the value by using the function avg() it returns me this error: ERROR: function avg(boolean) does not exist

How can I measure the event frequency and at the some time keep a good performance ?

Thank you.

Joao

like image 727
joaoavf Avatar asked Nov 20 '10 12:11

joaoavf


3 Answers

This will coerce the value into 0's and 1's

select avg(val::int);
like image 199
nate c Avatar answered Oct 11 '22 16:10

nate c


You could do this:

AVG(CASE WHEN  myBooleanAttribute = TRUE THEN 1 ELSE 0 END)
like image 24
jira Avatar answered Oct 11 '22 16:10

jira


If you cast as int you will get an int average, which isnt very useful. Instead try:

select avg(val::int::float4);
like image 41
Chris.Caldwell Avatar answered Oct 11 '22 17:10

Chris.Caldwell