Question About Aggregate Functions

From: "Don Parris" <parrisdc(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Question About Aggregate Functions
Date: 2006-09-12 16:47:03
Message-ID: 1eba300b0609120947g4ad9890ay92a3bb4a2799afc5@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

Greetings,

I'm a DB novice as well as a pgsql novice. I can manage to run a few basic
queries, but that's all I've really done so far. How do I create a query
that (1) evaluates each boolean field for TRUE/FALSE and (2) counts the
number of rows where each field is TRUE? Also, one field is an integer, so
I want the average from that field, rather than the count. Something along
the lines of:

SELECT COUNT (t1.fielda, t2.fielda, t2.fieldb) AVG(t2.fieldc) FROM t1, t2
WHERE t1.item_id=t2.item_id AND t1.fielda IS TRUE AND t2.fielda IS TRUE AND
t2.fieldb IS TRUE AND t2.fieldc IS NOT NULL

The result is intended to be something of a summary report. t1 contains
basic info about each item, with one field for whether the item is active or
not. The other table contains additional info about whether each item has
particular attributes. I want the query to tell me the average number of
years active items have been active, and the number of items where each
attribute is true. I then want to turn the raw attribute counts into
percentages, so I can say "for n% of the items these attributes are true".

Pointers to good examples/tutorials are welcome. Most I have seen are
rather simplistic, and what I am reading in the manual isn't coming together
very well.

Thanks,
Don

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-09-12 16:47:17 Re: Superuser lost access to particular database
Previous Message Tony Caduto 2006-09-12 16:40:25 Re: Stored Procedure performance / elegance question

Browse pgsql-novice by date

  From Date Subject
Next Message Don Parris 2006-09-13 01:15:37 Re: Question About Aggregate Functions
Previous Message Elias Chavarria 2006-09-12 15:55:08 Startup script problem