Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l6j4M-0007cX-Gb for pgsql-general@arkaria.postgresql.org; Mon, 01 Feb 2021 23:57:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l6j4L-0008Rk-FW for pgsql-general@arkaria.postgresql.org; Mon, 01 Feb 2021 23:57:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l6j4L-0008Rc-8D for pgsql-general@lists.postgresql.org; Mon, 01 Feb 2021 23:57:21 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l6j4J-0001nm-DT for pgsql-general@lists.postgresql.org; Mon, 01 Feb 2021 23:57:20 +0000 Received: by mail-pl1-x62b.google.com with SMTP id j11so8586698plt.11 for ; Mon, 01 Feb 2021 15:57:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=P+z/u4h4xAwk/E88id/D90dYVTQt+AS+XqlIGGsuTp8=; b=in19MFrRqFhKQ+OS0783uHLhM+GlmeLAt5oaBxEZfqEeM53ERyRmkwwAPzhiBMijFz ZQMLrtxF5VgKx6CRWZhbf69CWd9tNvezaH74WQBWDvVE3FEoiYn8A6K2JYjFmcl4UwWf yha4Nx0A+wLe90isg9FMsX1jvxuQHBXLEIvo/+5PTVd8UUHkZbLc9Jh6BDVRp6Ai925p N5sjg6GFmefX4ACeAXed/jpkGLDm7mmkvJZXktQVZ6fDhMcGpDlldPZdPTrdP9zETEK0 Ak/Kw8rgrNu+i9PxsOy8xcAZgUzehidk3sfR5MVcuCYeIYCivOO07AwUAtE7AmoP8gtR LjXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=P+z/u4h4xAwk/E88id/D90dYVTQt+AS+XqlIGGsuTp8=; b=tPEmrn7OUZOrrbzew2EyHG6XTNc/Me0ARfacjSm5HwlT7sss5fHgAJe2bvo0e+1QQ7 ++XIEs2IaJYrij1kkH9ZYQcFQx5nxnTBrFImF4dz++AjID64udrbqGd2q4+SOCv6Xn5k wouo8WNi7U38PUZ6/e5MVB31nO34c8YcgBPsmA8t+H4ghPAAcQJO2aCUNkyM5xcc95PP trRYCVtwaE/gPzXXY/UTNZk1jZpPAPxyuy5BI4fFSyWvpRM1AzqJWhlg31JK7S3JvnSZ /mW2KaN0CdA0UlHchXdffI6dcULU72GRLnJhdr6Pzeq0uds1FwuArb1qzHb79MwnbsKW bLdQ== X-Gm-Message-State: AOAM532UdfVu5mjErC9OTEYVy2G1ZFhTDwG0ODhj0bLCRrRiMRJkpFAe IQRQJN3W0/vWTTdU/+c4Z95WvdbHJXA= X-Google-Smtp-Source: ABdhPJz+AfwLhMQsbr4u+TyXIhhljNAoPq1YFlHFLaV1zeXBFckgZOyXaUaZnq4dOodIISYbaeFTKQ== X-Received: by 2002:a17:90a:b702:: with SMTP id l2mr1312330pjr.13.1612223837146; Mon, 01 Feb 2021 15:57:17 -0800 (PST) Received: from [10.0.0.94] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id g17sm20060682pgg.78.2021.02.01.15.57.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Feb 2021 15:57:16 -0800 (PST) Subject: Re: count(*) vs count(id) To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: Date: Mon, 1 Feb 2021 16:55:05 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.