I have a marketing table with a customer ID, email date, and a close date (when the sale is closed). I have a filter that basically says the closed date must occur within 30 days of the email date. The problem is that in certain cases where the close date occurs within 30 days of the email_date, I am double counting. I want the count to only show a 1 for the most recent email date, and never to double count.

Sample query to give you an idea:

DATE_TRUNC('month', email_date)
,count(distinct case when closed_date - email-date <= interval '30 days' then customer_id else null end)

Output Example Showing the double counting Problem:

Customer ID Email Date Count(distinct) Closed_Date

123 2015-05-01 1 2015-06-04
123 2015-06-01 1 2015-06-04

I've tried adding a filter like this:

date_trunc('month', email_date) = date_trunc('month', 'close_date)

the problem is that the close date can theoretically occur in '2015-07-01' because it's possible an email was sent towards the end of a month.