pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: count(*) vs count(id)
Date: Mon, 1 Feb 2021 16:55:05 -0700
Message-ID: <cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com> (raw)
In-Reply-To: <CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com>
References: <CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com>



On 2/1/21 4:53 PM, Matt Zagrabelny wrote:
> Greetings,
> 
> Is there a preferred method of counting rows?
> 
> count(*) vs count(field_name)
> 
> I have used count(*) for a long time and am hitting an inconsistency. At 
> least it is an inconsistency to me (I'm sure there is an explanation)...
> 
> INCORRECT OUTPUT (not desired results)
> $ select calendar.entry, count(*)  from 
> generate_series('2020-01-01'::date, '2021-01-10'::date, '1 
> day'::interval) as calendar(entry) left join call_records on 
> calendar.entry = call_records.timestamp::date group by calendar.entry;
> [...]
>   2020-08-30 00:00:00-05 │     1
>   2020-08-31 00:00:00-05 │     1
> [...]
> 
> CORRECT OUTPUT (desired results)
> $ select calendar.entry, count(id)  from 
> generate_series('2020-01-01'::date, '2021-01-10'::date, '1 
> day'::interval) as calendar(entry) left join call_records on 
> calendar.entry = call_records.timestamp::date group by calendar.entry;
> [...]
>   2020-08-30 00:00:00-05 │     0
>   2020-08-31 00:00:00-05 │     0
> [...]
> 
> What am I missing between count(*) and count(id)?
> 
> Thanks for any help!
> 
> -m

You got one null from count(*) likely.





view thread (14+ messages)  latest in thread

Message-ID: <cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com>
Permalink:  ../cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com/
Also on:    postgresql.org/message-id/cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: count(*) vs count(id)
  In-Reply-To: <cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com>
  References: <CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com> <cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com>

* 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