pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Lewis <mlewis@entrata.com>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: Counting number of sites with same number of sampling dates
Date: Wed, 11 Dec 2019 13:58:35 -0700
Message-ID: <CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.gmail.com> (raw)
In-Reply-To: <alpine.LNX.2.20.1912111226300.10822@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.1912111226300.10822@salmo.appl-ecosys.com>

On Wed, Dec 11, 2019 at 1:54 PM Rich Shepard <rshepard@appl-ecosys.com>
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;

view thread (4+ messages)  latest in thread

Message-ID: <CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.gmail.com>
Permalink:  ../CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.gmail.com/
Also on:    postgresql.org/message-id/CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.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: mlewis@entrata.com, rshepard@appl-ecosys.com
  Subject: Re: Counting number of sites with same number of sampling dates
  In-Reply-To: <CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.gmail.com>
  References: <alpine.LNX.2.20.1912111226300.10822@salmo.appl-ecosys.com> <CAHOFxGr4FZEsb5LHTec22UZZ+hXuX6ZbOH-R8Cj2u7OG43uTaw@mail.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