0

I am trying to get a count of rows where the earliest date of a given field falls within a range. What I am looking for is this:

SELECT  COUNT(AccountId) FROM Opportunity
WHERE StageName ='Posted'
AND RecordTypeID = '012800000002KPtAAM'
AND  min(CloseDate) > 2015-09-01
AND min(closeDate) < 2016-08-31

It fails miserably.

1

Here is your update question:

I am looking for a count of the rows where the First close date fell within a date range. I'm trying to extract the number of people who made their first donation within a given time frame.

The above wasn't well laid out in your OP, so here is an updated answer to reflect your actual question. You need to use an aggregate query. Take a look at how the HAVING and GROUP BY keywords work.

SELECT AccountId FROM Opportunity
WHERE StageName = 'Posted'
    AND RecordType.DeveloperName = 'Some_DeveloperName'
GROUP BY AccountId
HAVING MIN(CloseDate) > 2015-09-01
    AND MIN(CloseDate) < 2016-08-31
  • Each account has multiple close dates, one for each donation. I am looking for a count of the rows where the First close date fell within a date range. I'm trying to extract the number of people who made their first donation within a given time frame. – Riley Apr 26 '16 at 21:01
  • Adrian, you really gave me the pieces I needed to make this work. Thank you so much. This is the statement that worked: "SELECT AccountId FROM Opportunity WHERE StageName = 'Posted' AND RecordTypeID = '012800000002KPtAAM' GROUP BY AccountId HAVING MIN(CloseDate) >= 2013-09-01 AND MIN(CloseDate) <= 2014-08-31"; – Riley Apr 26 '16 at 21:35
  • The number of results gives me the number I was looking for. The last piece I have is, is there a way to use COUNT to get that number, rather than using the number of results? – Riley Apr 26 '16 at 21:36
  • @Riley I don't think so with pure SOQL. – Adrian Larson Apr 26 '16 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.