RE: Distinct Counts

From: Chris Campbell <ccampbell(at)cascadeds(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: RE: Distinct Counts
Date: 2018-04-19 13:34:47
Message-ID: BYAPR11MB2677A713FD3D95780D40B97AABB50@BYAPR11MB2677.namprd11.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Select contact.accountname,
(Select
Count(env.fk_campaignname)
FROM ds04.campaignname
INNER JOIN ds04.envelope env ON env.fk_campaignname = campaignname.pklkey
INNER JOIN ds04.pledge plg ON env.envelopekey = plg.fk_envelope
WHERE plg.fk_contact = contact.contactkey
) AS reccount
FROM ds04.contact
WHERE contact.contactkey = 23460
GROUP BY contact.accountname, contact.contactkey
ORDER BY contact.accountname

This works fine except I’d like to change it so that the “Count()” only returns the count of unique keys. So if this account has more than one record for a given env.fk_campaignname I only want it to count as one occurance.

​(Select Count( DISTINCT env.fk_campaignname) ...

https://www.postgresql.org/docs/10/static/sql-expressions.html#SYNTAX-AGGREGATES

David J.

Thank you David. That did the trick.

Chris

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Ian Bell 2018-04-20 00:24:35 unable to read composite types from CSV files
Previous Message David G. Johnston 2018-04-18 22:26:35 Re: Distinct Counts