You need to sign in to do that
Don't have an account?
SOQL Grouping and Ordering in same statement
I am stuck on writing a query to facilitate Congra Composer. For reasons beyond this request, I need to 'dedupe' a query of data as well as put it into a set order. I can easily do one or the other but not both. If I do an ORDER BY I get all results using the following SOQL:
SELECT Group__c, Order__c FROM OpportunityLineItem WHERE Opportunity.Id = 'xxxxxxxxxxxx' Order by Order__c
Which returns:
1 Eggs
1 Eggs
2 Tomatoes
2 Tomatoes
3 Bananas
3 Bananas
However that does not allow me to 'dedupe' as i would if I used the following SOQL:
SELECT Group__c FROM OpportunityLineItem WHERE Opportunity.Id = 'xxxxxxxxxxx' GROUP BY Group__c
Which returns:
Eggs
Tomatoes
Bananas
So... When I put it all together in hopes of getting (1 Eggs, 2 Tomatoes, 3 Bananas) with the following SOQL:
SELECT MAX(Order__c), Group__c FROM OpportunityLineItem WHERE Opportunity.Id = '0064000000Rw0ea' GROUP BY Group__c ORDER BY Order__c
I get various "MALFORMED_QUERY: Ordered field must be grouped or aggregated: Order__c" Errors
Any ideas on how to achieve an ordered and distinct (deduped) query?
You need to add all the fields you are selecting to your Group By in SOQL statement for your SOQL to work. Please add Order__c field also to your Group BY and see if it works.
Thank you
Any field thats included in Aggregate SOQL has to be either aggregated or grouped that's the key here.
Hope this helps !!
MALFORMED_QUERY:
'0064000000Rw0ea' GROUP BY Group__c,Order__c,OpportunityId ORDER BY
^
ERROR at Row:1:Column:110
field 'Order__c' can not be grouped in a query call
Order__c is type=number
like if say we have c1 created on a1 on 25/09
c2 created on a1 on 30/09
c4 created on a2 on 27/09
my result set should return:
c2 created on a1 on 30/09
c4 created on a2 on 27/09
SELECT Account__c,DateTime__c FROM Call__c group by Account__c,DateTime__c ORDER BY DateTime__c DESC NULLS LAST LIMIT 1
I am getting same error AS:
MALFORMED_QUERY:
Call2__c group by Account__c,DateTime__c ORDER
^
ERROR at Row:1:
field 'DateTime__c' can not be grouped in a query call
any idea how to achieve this data in the single query?
But I dont have my record identifier so that I can use the id field to access other fields of record in further list collections,
SELECT MAX(DateTime__c),Account_vod__c FROM Call__c where Status__c = 'Submitted_vod' AND DateTime__c >= LAST_N_DAYS:7 and Accountc !=null group BY Accoun__c