You need to sign in to do that
Don't have an account?
SOQL Count - MALFORMED_QUERY: Grouped query cannot use child relationships in the SELECT list
Hi All,
I have the following query which I have tested and works in SoqlXplorer.
SELECT Member__c, (SELECT Joined_Date__c,Joined__c FROM Opportunities__r WHERE Joined_Date__c = LAST_N_DAYS:365) FROM Contact WHERE Member__c = TRUE LIMIT 1000
However, my issue is I want to count the results ie like the below but this query results in MALFORMED_QUERY: Grouped query cannot use child relationships in the SELECT list
SELECT Count(ID), (SELECT Joined_Date__c,Joined__c FROM Opportunities__r WHERE Joined_Date__c = LAST_N_DAYS:365) FROM Contact WHERE Member__c = TRUE LIMIT 1000
Can anyone help.
Thanks
Kev
You can get the single count by removing group by clause and aggregated Join_Date__c field as below
SELECT COUNT_DISTINCT(Opportunity_Contact__c) From Opportunity Where Joined_Date__c = LAST_N_DAYS:365 AND Opportunity_Contact__r.Member__c = TRUE
I also changed count to count_distinct as there could be opportunities with same contact.
All Answers
Can you try and rewrite the query as follows
SELECT Count(Contact__c), Joined_Date__c From Opportunities Where Joined_Date__c = LAST_N_DAYS:365 AND Contact__r.Member__c = TRUE Group By Joined_Date__c
Let me know if you get the desired result
Sorry I get the error invalid field Contact__c
This is because Contact__c doesn't exist in the Opportunity Object.
What's the contact lookup field in opportunities__c object? replace that with contact__c
OK just looked at the structure and it seems to be Opportunity_Contact__c
So changed the below to this:
SELECT Count(Opportunity_Contact__c), Joined_Date__c From Opportunity Where Joined_Date__c = LAST_N_DAYS:365 AND Contact__r.Member__c = TRUE Group By Joined_Date__c
However, now get the error doesn't understand relationship Contact__r
Thanks
Kev
You need to change it inside as well,
SELECT Count(Opportunity_Contact__c), Joined_Date__c From Opportunity Where Joined_Date__c = LAST_N_DAYS:365 AND Opportunity_Contact__r.Member__c = TRUE Group By Joined_Date__c
Thank you that works, however, how do I get a single count as the Group By causes it to count results for each Joined_Date and I want the total for the last 365 days.
You can get the single count by removing group by clause and aggregated Join_Date__c field as below
SELECT COUNT_DISTINCT(Opportunity_Contact__c) From Opportunity Where Joined_Date__c = LAST_N_DAYS:365 AND Opportunity_Contact__r.Member__c = TRUE
I also changed count to count_distinct as there could be opportunities with same contact.
Thank you so much that was what I was looking for.
Well I'm had to change the soql to this:
I'm pulling the ownerid from a drop down now, however, the above gives me the error Illegal assignment from Integer to SOBJECT:AggregateResult
Any help would be appreciated.
Have you tried
That worked when I was quering the current user, however, now that I am putting the :userid in it's place I get the error.
Looking at the log it shows the below, looks like :userid is being changed to :tmpVar1 and that is then saying it found no rows.
Not sure why!!
That's strange as the query is supposed to return 1 row always, if records are not found, it returns 0. Could you put a system.debug message to display value of userid
The strange thing is I have the below query which works and that uses the userid so I know it is being passed to the apex class.
Thinking about it the userid isn't passed until the page is loaded and the dropdown box is selected so to start with the result would be 0.
Got it!! The issue was because userid was blank - thank you so much.
I've changed the code to this and it now works.
Just got to work out how to write the testmethod for it.