Thread: avg() from multiple columns

avg() from multiple columns

From
Miia Uski
Date:
I have a question regarding taking the average value from several
columns.

I am trying to evaluate a survey with sql. I have one column per
question, and one row per answered survey. The answers can have values 1

- 4, unanswered questions have 0. I want to take the average from the
answers of several questions that belong to the same category. For one
question I can do like this:

igv=> select avg("1.2") from igvdata
igv-> where "1.2" > 0;

How do I go about to have averages for several columns? Do I have to
create views in order to avoid the "no answer"-values for each column?


Thanks in advance,

Miia




Re: avg() from multiple columns

From
Jason Tan
Date:

If you want row by row averages try this:

select (col1+col2+col3)/3 from table;

If you wnat to average several columns over the whole table this seems to
work:

select avg(col1+col2+col3) from table;

Regards
Jason

On Sun, 2 Sep 2001, Miia Uski wrote:

> I have a question regarding taking the average value from several
> columns.
>
> I am trying to evaluate a survey with sql. I have one column per
> question, and one row per answered survey. The answers can have values 1
>
> - 4, unanswered questions have 0. I want to take the average from the
> answers of several questions that belong to the same category. For one
> question I can do like this:
>
> igv=> select avg("1.2") from igvdata
> igv-> where "1.2" > 0;
>
> How do I go about to have averages for several columns? Do I have to
> create views in order to avoid the "no answer"-values for each column?
>
>
> Thanks in advance,
>
> Miia
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>

--
------------------------------------------------------------------------------
Jason Tan                                                jason@rebel.net.au
     "Democracy is two wolves and a lamb voting on what to have for lunch.
                 Liberty is a well-armed lamb contesting the vote."
                               ~Benjamin Franklin, 1759
------------------------------------------------------------------------------


Re: avg() from multiple columns

From
"Oliver Elphick"
Date:
Jason Tan wrote:
  >
  >
  >If you want row by row averages try this:
  >
  >select (col1+col2+col3)/3 from table;
  >
  >If you wnat to average several columns over the whole table this seems to
  >work:
  >
  >select avg(col1+col2+col3) from table;

Watch out for NULL values when doing this.  x + x + NULL = NULL, not 2x

You might do better to do:

  SELECT avg(col1) + avg(col2) + avg(col3)

or

  SELECT avg(COALESCE(col1, 0) + COALESCE(col2, 0) + COALESCE(col3,0))

(the two are not equivalent).

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Behold, I stand at the door, and knock; if any man
      hear my voice, and open the door, I will come in to
      him, and will sup with him, and he with me."
                                   Revelation 3:20