Include other fields in the output of an aggregate using group by
2644
Views
8
Comments
New
Aggregates & Queries

When using group by in an aggregate, you can only add a Max/Min/SUM of another attribute. For example I would like to add username to the output of this aggregate.


Is it ridiculous to ask for attributes to be draggable and can be included in the output. 
Otherwise, and in my case, I have a lot of entities related to each other and a lot of mapping should be done on each screen = A lot of aggregate for few fields..

Hi Nabil,


If I understood correctly, you want to add the username or other entity attribute into the output.
You can group more than one attribute into the output on a group by aggregate, although the number of lines may change depending on the number of entities, etc.

Thank you!

Hi Joao,

I do not want to group by other attributes. I want to group by Survey Id the usernames or any other fields.
I want the results to be something like this:
Output:
Group by Survey Id         Username      Question Details
              1                                Nabil                 Who Are you ?

                                                --------------------------------------------
                                                 John                  Who are we ?
-----------------------------------------------------------------------------------
               2                               Sam                   What?
                                                 Angel                Who ? 
So it's not about group by multiple attributes. It's about displaying other fields alongside group by 

Hi Nabil,


Aggregate is an abstraction for the SQL query and in SQL when you want to use the Group by clause, all the fields in the Select should match the fields in the group by.
You can add the SurveyId, Username and Question Details as Group fields in the aggregate and you will have the following result:


However to achieve that result, you can create 2 structures for example:
stUserQuestion 

User Id

UserName

Question


stSurvey

SurveyId

stUserQuestionList 

You run 2 aggregates:

1st  with the list of the Grouped Surveys

Append data to the stSurvey

2nd with the list of users

 Append data to the stUserQuestionList under the current stSurvey.

Hope it helps!

I believe this is possible as João mentions, and yes the data will be repeated, but you can manage easily in your list or table not to show a value if it is the same as the previous row.

@João Henriques :
Hello again,
To make everything clear. For example I have a use case where I want to include username and survey title group by SurveyId as shown here( they have N to N relationship so this is an another entity: TakeSurvey)If I need to display users who passed the surveys grouped by a survey, Do I set UserName and survey title in the groupby and create a structure to include that result of aggregate ? And if yes, how to map that for example to start time and finish time for each survey.
@Daniël Kuhlmann
So if I use group by in all required fields, I need to create a function which take the list as input and check weather the current element is equal to the previous( of course I need to take care of the first element) and not display it when the condition is true? 
I have this kind of use case a lot and I think this is kind of a nightmare for apps having a lot of N to N relationships ...

By creating the function, I meant checking current element in the list with element with index = CurrentRowNumber - 1. Not a function, just an if condition in the expression when displaying the element.

Hi @Nabil Kriden
 
To accomplish that you can create an aggregate with the tables Survey, TakeSurvey and User.
Then you select the fields that you want to group, in this case, SurveyId, SurveyTitle and Username.

The result should be for example:

Survey Id | SurveyTitle | Username

1                |Title A           | User A

1                |Title A           | User B 

...

Thank you

To achieve the result that you initially mentioned, you need to some additional process in the Outsystems flow after the aggregate.