Re: SQL Question - Using Group By

From: "Mike" <akiany(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Question - Using Group By
Date: 2007-02-23 23:12:58
Message-ID: 1172272378.356319.11660@z35g2000cwz.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you! Exactly what I needed.
Mike

On Feb 23, 4:42 pm, "ksherl(dot)(dot)(dot)(at)gmail(dot)com" <ksherl(dot)(dot)(dot)(at)gmail(dot)com> wrote:
> You could use COUNT() in conjunction with NULLIF:
>
> select "Type",
> count(nullif("Active", false)) as "Active Count",
> count(nullif("Active", true)) as "Inactive Count",
> 100 * count(nullif("Active", false)) / count(*) as "Active Percent"
> from table_name group by "Type"
>
> On Feb 23, 2:50 pm, "Mike" <aki(dot)(dot)(dot)(at)gmail(dot)com> wrote:
>
> > Hi,
>
> > I have a question about using Group By.
>
> > On a table like this:
>
> > Type (varchar) | Active (boolean)
> > --------------------------------------------------------
> > Type One | False
> > Type Two | True
> > Type One | True
> > Type Fifty | Flase
> > Type Two | True
>
> > Having this table I want a report grouping Types and giving me more
> > statistics such as:
>
> > Type | Active Count | Inactive Count | Active
> > Percent
>
> > How do i do that?
>
> > I can think of :
>
> > select Type from table_name group by Type
>
> > But that doesn't give me how many active and inactive each had!
>
> > Please help me here understand how to approach this.
>
> > Thank you,
> > Mike

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-02-23 23:14:25 Re: [HACKERS] urgent: upgraded to 8.2, getting kernel panics
Previous Message Peter Eisentraut 2007-02-23 23:12:25 Re: postgresql vs mysql