+ Start a Discussion

Error: duplicate alias: Name in AggregateResult



I am trying to retrieve the list of users grouping by their role names:

The output should be something like:


Role A

   User A

   User B

Role B

   User C

   User D


I used the following query:

list<AggregateResult> listAr  = [select  UserRole.Name,name from User where  IsActive =true group by UserRole.Name];


but it is showing the duplicate alias: Name error.


How can I achieve this?


Thanks in advance!!




'Group by' is used when you have to perform operations like sum, min max on some field and result being grouped based on  your 'group by' field . I dont think, your query is valid.

If you want to show result  in VF page that you can try what is mentioned in the following post . This post states different problem but achieve what you have mentioned.




Thanks !


I referred to that blog,


But, I want to display the Role name only once.And  the o/p Should be like:



   User A

   User B




 [select  UserRole.Name,name from User where .....


AggregateResult found double name in it. just make those different by alias:


 [select  UserRole.Name,name name2 from User where .....


Hope this help.