# QueryBuilder get existing joins?

**URL:** <https://forum.servoy.com/t/querybuilder-get-existing-joins/18147>\
**Category:** Classic Servoy\
**Created:** [September 2, 2014, 3:29am UTC](https://forum.servoy.com/t/querybuilder-get-existing-joins/18147 "2014-09-02T03:29:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maria.kyselova](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@maria.kyselova](https://forum.servoy.com/u/maria.kyselova)\
**Post date:** [September 2, 2014, 3:29am UTC](https://forum.servoy.com/t/querybuilder-get-existing-joins/18147/1 "2014-09-02T03:29:53Z")

</div>

Hi All,

I ran into a problem adding group-by clauses to my query.  
What I do it just this:

```auto
var qbSelect = foundSet.getQuery();
var newJoin = qbSelect.joins.add(relationName, aliasName);
var qbColumn = newJoin.getColumn(groupingColumnName);
qbSelect.groupBy.add(qbColumn);

```

All works well if my original foundset doesn’t have a join to the table I’m adding a grouping on.  
So, in a scenario when I want to group assignments by employee code from employee table, I get the following:

My original foundset query is something like this, it’s already got the join to employees:  
‘SELECT idassignment from assignment join employee on assignment.idemployee = employee.idemployee Order by assignment.idassignment’.

When I add my join via ```  
qbSelect.joins.add(relationName, relatedColumnName);

```and

```

column “employee.employeecode” must appear in the GROUP BY clause or be used in an aggregate function

```auto

The error message also has the query I"m trying to load and it's like this:

```

select assignment.idassignment  
from assignment assignment  
left outer join employee employee on assignment.idemployee=employee.idemployee  
left outer join employee employeecode on assignment.idemployee=employeecode.idemployee  
group by assignment.idassignment , assignment.startdate , employeecode.employeecode  
order by assignment.startdate asc, employee.employeecode asc, assignment.idassignment asc

```auto

So it looks like I'm adding a join with alias 'employeecode', the query builder adds that field to the group-by clause. Which is fine.
But at that point the order-by clause already has a reference to the same column from the same table but the other alias: 'employee.employeecode'. 

I tried to clear the qbSelect.sort and add my own column to it, but it just doesn't work, it sticks that column there anyway.

How do I figure this out?
```
