Re: COUNT and GROUP BY performing strangely

From: James David Smith <james(dot)david(dot)smith(at)gmail(dot)com>
To: Jayadevan M <maymala(dot)jayadevan(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: COUNT and GROUP BY performing strangely
Date: 2013-10-25 11:24:44
Message-ID: CAMu32AAyMa2meGC8QGvLYajj+pF+tTsKkvHsPS18zRCc5Oz1TQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Thanks. I never realized it behaved like that. Will correct queries.
On 25 Oct 2013 12:22, "Jayadevan M" <maymala(dot)jayadevan(at)gmail(dot)com> wrote:

>
> http://stackoverflow.com/questions/2054235/why-doesnt-postgres-group-by-null-select-counts
> http://www.postgresql.org/docs/9.0/static/functions-aggregate.html
> says "number of input rows for which the value ofexpression is not null"
>
>
> On Fri, Oct 25, 2013 at 3:38 PM, James David Smith <
> james(dot)david(dot)smith(at)gmail(dot)com> wrote:
>
>> Hi everyone,
>>
>> Any thoughts on the below three queries please?
>>
>> 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 ?
>>
>> Thanks
>>
>> James
>>
>>
>> --
>> Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-novice
>>
>
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message James David Smith 2013-10-28 11:58:11 Import from CSV error
Previous Message Jayadevan M 2013-10-25 11:22:19 Re: COUNT and GROUP BY performing strangely