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
buggs sfdcbuggs sfdc 

SOQL Query on Account Opportunity to get the data from 2010 to 2016

HI

Can any one help me out in getting total count of opportunites between this years 2010 to 2016 which also needs accountname.

Thanks in Advance
select id, Name, (Select id, Accountid, Createddate, stageName From Opportunities) From Account where stageName = '6 - Closed Won' AND createdDate > 2010-01-01T00:00:00Z and createdDate < 2016-01-31T23:59:59Z

 
Best Answer chosen by buggs sfdc
Dhanya NDhanya N
Hi,
Try this query:
Select id, Accountid, Account.Name, Createddate, stageName From Opportunity WHERE createdDate > 2010-01-01T00:00:00Z and createdDate < 2016-01-31T23:59:59Z AND StageName = '6 - Closed Won' ORDER BY Account.Name
Thanks,
Dhanya
 

All Answers

Dhanya NDhanya N
Hi,
Try this query
Select id, Accountid, Account.Name, Createddate, stageName From Opportunity WHERE CALENDAR_YEAR(createdDate) > 2010 and CALENDAR_YEAR(createdDate) < 2016 ORDER BY Account.Name
Thanks,
Dhanya
buggs sfdcbuggs sfdc
HI Dhanya

Thanks for the quick response,i also nee to query the tageName = '6 - Closed Won' with the which includes YY-MM-DD in the query,i tried replacing in above query its not workig,can you help me out with the same.
Dhanya NDhanya N
Hi,
Try this query:
Select id, Accountid, Account.Name, Createddate, stageName From Opportunity WHERE createdDate > 2010-01-01T00:00:00Z and createdDate < 2016-01-31T23:59:59Z AND StageName = '6 - Closed Won' ORDER BY Account.Name
Thanks,
Dhanya
 
This was selected as the best answer
Adeel Ahmad 17Adeel Ahmad 17
Hi,
You can try this one. To get the count, you must have to assign it to list and then get the list size, it will show total number of records otherwise you have to write dynamic SOQL to get the total count.
 
Select id, Accountid, Account.Name, Createddate, stageName From Opportunity WHERE stageName = '6 - Closed Won' AND CALENDAR_YEAR(createdDate) >= 2010 AND CALENDAR_YEAR(createdDate) <= 2016 ORDER BY Account.Name


Thanks!
Adeel
buggs sfdcbuggs sfdc
Thank You both of you,Two queries is working like charm.
zuber Rangreazzuber Rangreaz
    List<Account> accList = [SELECT Id, Name, Fax, (SELECT Id, StageName FROM Opportunities) FROM Account WHERE id =: accid];