function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vinny12Vinny12 

SQL query for salesforce objects

Hi,

 

I have a Contact and Opportunity(Donations) objects.

Opportunity is child obj for Contact.

 

In our case,  for one contact there may be several opportunity( donation) records. 

 

Close date and Amount are two fields in opportunity.

 

How to query for a condition in sql, most recent close date and most recent amount ? for a given date range and given amount range.

 

select  name,max(date),amount

from donations

where closedate> '2011-3-4' and closedate < '2012-2-4' and amount>10 and amount<100

group by name

 

but it throws me an error on amount field as it is not in aggregate function.

 

how to modify this query such that for each contact record, it pulls donation's recent close date and recent amount. ( Consider for 1 contact we have 10 donations. so we need to pull 10th donation details.)

AsiereikiAsiereiki

select  name,max(date),amount

from donations

where closedate> '2011-3-4' and closedate < '2012-2-4' and amount>10 and amount<100

group by name,amount

 

 

you need to group or aggregate every field you requested in the query or it fails.

AsiereikiAsiereiki

Hi,

 

Did my answer helped you?