From: Oleg Lebedev <olebedev@waterford.org>
To: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Postgres SQL Mailing List <pgsql-sql@postgresql.org>
Subject: Re: COUNT func
Date: Thu, 18 Oct 2001 17:04:25 -0600
Message-ID: <3BCF5FF9.258EAC36@waterford.org> (raw)
References: <Pine.BSF.4.21.0110181538210.28566-100000@megazone23.bigpanda.com>
It worked!
I checked the query plan it generates, and it's really a one-pass scan.
thanks,
Oleg
Stephan Szabo wrote:
> On Thu, 18 Oct 2001, Oleg Lebedev wrote:
>
> > Hi,
> > I am trying to count the number or rows in a table with similar field
> > values. I want to do it in one table scan.
> > In other words, say I want to count the number of rows in Person table,
> > having age equal to 40, and the number of rows with status as 'married'.
> > I want Person table to be scanned only once during this query.
> > So, basically I want to do the following:
> > SELECT COUNT(age=40), COUNT(status='married')
> > FROM Person;
> >
> > I tried to use aggregate functions with group by and having clauses, but
> > I still can't figure it out (probably because of the lack of
> > experience).
>
> Maybe something like [untested]
> SELECT SUM(case when age=40 then 1 else 0 end),
> SUM(case when status='married' then 1 else 0 end)
> FROM PERSON;
> will work?
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: olebedev@waterford.org, sszabo@megazone23.bigpanda.com
Subject: Re: COUNT func
In-Reply-To: <3BCF5FF9.258EAC36@waterford.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox