0

So I have the following SOQL Query:

SELECT Id, Name, Week_Name__c, Contact__c, Contact__r.Name, SUM(Hours_Worked_Rollup__c) hours, Status__c, Unique_Id__c, Week_Start_Date__c, Approver__c, Approver__r.Name, Project_Name__c, Contact_Name__c, Status_Indicator__c, Project_Name_Clone__c, EmployeeId__c, Project_Contact__c, ProjectId__c, Project_Contact__r.Project__r.Client__r.Name, Project_Contact__r.Project__r.Project_Name__c FROM Time_Sheet__c GROUP BY Contact__c.Name

when I try to run it I get an error that says "duplicate alias: name",

does anybody know why I'm getting this error ? It only happens when I use "Group By".

3
  • What are you trying to achieve here? You can't select non-aggregated fields in a grouped query, and the WHERE clause doesn't make sense (Id can never be null or blank).
    – David Reed
    Commented Oct 5, 2020 at 14:53
  • @DavidReed I'm sorry, this is the actual query: SELECT Id, Name, Week_Name__c, Contact__c, Contact__r.Name, SUM(Hours_Worked_Rollup__c) hours, Status__c, Unique_Id__c, Week_Start_Date__c, Approver__c, Approver__r.Name, Project_Name__c, Contact_Name__c, Status_Indicator__c, Project_Name_Clone__c, EmployeeId__c, Project_Contact__c, ProjectId__c, Project_Contact__r.Project__r.Client__r.Name, Project_Contact__r.Project__r.Project_Name__c FROM Time_Sheet__c GROUP BY Contact__r.Name
    – peepoCode
    Commented Oct 5, 2020 at 15:24
  • @DavidReed I just modified the one in the question, my bad, but I'm still getting the same error
    – peepoCode
    Commented Oct 5, 2020 at 15:25

1 Answer 1

1

The text of the error is not particularly clear, but the problem is that you're making an aggregate query while trying to select fields that you have not grouped or aggregated. You cannot do that in an aggregate query, because you'll get back one row per grouped value (Contact__r.Name) and you haven't specified how to store the potentially multiple values are present in the underlying rows for fields like ProjectId__c.

Aggregate queries return AggregateResult objects, not sObjects. AggregateResults don't have nested object data like sObjects do, just named properties. Here, you are trying to select multiple fields across relationships whose API names are Name, which would collide with one another if you tried to store them in the same AggregateResult. That's why you get a rather inscrutable error message.

But fundamentally, you just cannot structure this query in this way.

To query for hours by employee, you'd have to do something like this:

SELECT Contact__c, SUM(Hours_Worked_Rollup__c) hours
FROM Time_Sheet__c 
GROUP BY Contact__c

Then your AggregateResult records will have two fields populated: Contact__c, the Id, and the hours rollup value. You can then take your Contact Ids and make a second, non-aggregated query to pull all of the other details that you require.

If you're trying to display all this data together on the front end, you'll often need to define a wrapper class to store the aggregate data along with the contact details.

2
  • Thanks for your answer @DavidReed, basically what I need to do is to query those fields and group them in a way so that the hours are shown per employee(for example, right now if I remove the group by clause, it would just show every timesheet(so if an employee submits multiple timesheets, then it would show all of them) and I want to just show 1 recor per employee with the total loged hours, How can I archieve this given the query that I just sent, I'm sorry, my english is kidna bad and I'm new to SF.
    – peepoCode
    Commented Oct 5, 2020 at 19:05
  • Su Ingles es mejor que mi Espanol :) I will edit my answer.
    – David Reed
    Commented Oct 5, 2020 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .