Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1if94e-0007iY-Tb for pgsql-general@arkaria.postgresql.org; Wed, 11 Dec 2019 20:59:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1if94d-0007zg-CN for pgsql-general@arkaria.postgresql.org; Wed, 11 Dec 2019 20:59:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1if94c-0007z4-T2 for pgsql-general@lists.postgresql.org; Wed, 11 Dec 2019 20:59:07 +0000 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1if94Z-0006HV-Nw for pgsql-general@lists.postgresql.org; Wed, 11 Dec 2019 20:59:06 +0000 Received: by mail-qt1-x832.google.com with SMTP id w47so263057qtk.4 for ; Wed, 11 Dec 2019 12:59:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=entrata-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=MAMcBbaN+ts2j4O8DbeF+wxzdr/Krfl1/cqGc+W+XAo=; b=yS9wBRME09XHmYYDiziCNBYJEKhI7XViGBh7bf1uFS2LDAfdXrRKCktDyRIRoQiNxO j3zG+qYXdAb1b+Ougd/3Le7PA5pbpkV/NH8vga+XFjzygug/HIiCObOZDIsCrVbVrB6G RV1H6Q9Fszg3GkrEQ0Nr4f8QfSDkvLbERc2HqmFRxcwX/ESAfuL+v6tXdIY521sPhzU1 8MffdsfoEwuTlJglqvmXtdwIi4W4gAsUaxMHuz7S6Ou7G0uO1lHXWuq4ewdbH2m3cDB1 V8yGzc7x32TVbq3fu3FiQ9auJJYVr/tzw9elEDh8hKclmBMfebOQdFazKjAE7aUwJ4qZ W8Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=MAMcBbaN+ts2j4O8DbeF+wxzdr/Krfl1/cqGc+W+XAo=; b=COo+CUGhVsmuGNBKHSGg79HYr3udb7301AxII6ydNUVsht46ZGOivfMiBm9Kb8vqzs 7l1m24EQ1YtHleX6rZx0hxSGzd3FGSaYKYg/gNb5dpvS3eN1fW0/o5MiUIsaFc4e2ibk +jCRNBbzZcAVWGtByYjL4wDMWUx0N0m7XCnLfq8YGx3w+s7L2ZqQFbfyI+YP0/+0BUoW LG6GGHZ71RCmER7XyK+H0zaNnpLtaLb+c9MMJHkZkRbkvzOMhbQEYQrju2/1YMEz3Q0L PydXAr7NxiOZR15Sb9F6ZsR10FxxA6b1vC2pvYhGApj6TquYeGC4E1N45ZuFkGUa1Yyu d1Gg== X-Gm-Message-State: APjAAAVylk4ZFMPNLe0Hy+j7ddV9KOdLlHSqArbzDEnMJrNMPFsvP92Z HBeBrBIagrlTsyFQhlAwWTWmsa6Jc/G+JpGedAs+/E/PJTlbAw== X-Google-Smtp-Source: APXvYqwkKi6cZiQSEGiBxRNuRsmaw1lkYpC7ZaFKX43tXrrvQmks6abLcPUIn9yyzVjE+AOMo3q01uDnxhTW18wQpiI= X-Received: by 2002:aed:3c48:: with SMTP id u8mr4594873qte.366.1576097941341; Wed, 11 Dec 2019 12:59:01 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Michael Lewis Date: Wed, 11 Dec 2019 13:58:35 -0700 Message-ID: Subject: Re: Counting number of sites with same number of sampling dates To: Rich Shepard Cc: PostgreSQL General Content-Type: multipart/alternative; boundary="000000000000ece0e4059973e503" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000ece0e4059973e503 Content-Type: text/plain; charset="UTF-8" On Wed, Dec 11, 2019 at 1:54 PM Rich Shepard wrote: > A sampling location table has 28 distinct sites, each site being sampled > from 1 to 67 times. I'm trying to obtain the number of sites having 1 > sample, 2 samples, ... 67 samples and am not seeing the solution despite > several alternative queries. > > The query, > > select site_nbr, count(distinct sampdate) from wrb_hg_cong group by > site_nbr order by site_nbr; > > returns the number of times each site has been sampled, for example: > > site_nbr | count > ----------+------- > 10332 | 11 > 10335 | 1 > 10339 | 2 > 10340 | 1 > 10342 | 4 > 10344 | 18 > 10347 | 2 > 10348 | 2 > 10350 | 2 > 10351 | 1 > 10355 | 14 > etc. > > I want the number of sites for each number of samples (e.g., how many sites > with one sampdate, 2 sampdates, etc.). I cannot group by an aggregate such > as count(distinct sampdate) yet I'm confident the syntax is simple and I'm > not seeing how to get the number in each group. > Put what you have in a subquery and group/aggregate again. select sample_count, count( site_nbr ) FROM ( select site_nbr, count(distinct sampdate) AS sample_count from wrb_hg_cong group by site_nbr order by site_nbr ) sub group by sample_count; --000000000000ece0e4059973e503 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Wed, Dec 11, 2019 at 1:54 PM Rich = Shepard <rshepard@appl-ecosy= s.com> wrote:
A sampling location table has 28 distinct sites, each site being sampl= ed
from 1 to 67 times. I'm trying to obtain the number of sites having 1 sample, 2 samples, ... 67 samples and am not seeing the solution despite several alternative queries.

The query,

select site_nbr, count(distinct sampdate) from wrb_hg_cong group by site_nb= r order by site_nbr;

returns the number of times each site has been sampled, for example:

site_nbr | count
----------+-------
=C2=A0 10332=C2=A0 =C2=A0 |=C2=A0 =C2=A0 11
=C2=A0 10335=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A01
=C2=A0 10339=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A02
=C2=A0 10340=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A01
=C2=A0 10342=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A04
=C2=A0 10344=C2=A0 =C2=A0 |=C2=A0 =C2=A0 18
=C2=A0 10347=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A02
=C2=A0 10348=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A02
=C2=A0 10350=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A02
=C2=A0 10351=C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A01
=C2=A0 10355=C2=A0 =C2=A0 |=C2=A0 =C2=A0 14
etc.

I want the number of sites for each number of samples (e.g., how many sites=
with one sampdate, 2 sampdates, etc.). I cannot group by an aggregate such<= br> as count(distinct sampdate) yet I'm confident the syntax is simple and = I'm
not seeing how to get the number in each group.


Put what you have in a subquery and group/aggregate= again.

select sample_count, count(=C2=A0site_nbr ) FRO= M (
select site_nbr, count(distinct sampdate) AS sample_count from = wrb_hg_cong group by site_nbr order by site_nbr
) sub
g= roup by=C2=A0sample_count;
--000000000000ece0e4059973e503--