Re: COUNT and GROUP BY performing strangely

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: COUNT and GROUP BY performing strangely
Date: 2013-10-25 10:26:41
Message-ID: l4dh0n$7l0$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

James David Smith, 25.10.2013 12:08:

> james_traffic=# SELECT count(*) FROM stage;
> count
> --------
> 430991
> (1 row)
>
> james_traffic=# SELECT bad_flag, count(bad_flag) FROM stage GROUP BY bad_flag;
> bad_flag | count
> ----------+-------
> | 0
> 1 | 4102
> (2 rows)
>
> james_traffic=# SELECT count(bad_flag) FROM stage;
> count
> -------
> 4102
> (1 row)
>
> I'm confused as to why the second query doesn't return this:
>
> bad_flag | count
> --------------+-------
> | 426889
> 1 | 4102
>
> It should count the records surely, even if the field is blank/null ?
>

This is because count(bad_flag) is equivalent to count(*) where bad_flag is not null

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Jayadevan M 2013-10-25 11:22:19 Re: COUNT and GROUP BY performing strangely
Previous Message James David Smith 2013-10-25 10:08:39 COUNT and GROUP BY performing strangely