Index: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java =================================================================== --- openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java (revision 564449) +++ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java (working copy) @@ -1373,9 +1373,12 @@ getJoins(joins, true); if (_grouping == null) _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - _grouping.append(sql); + + if (_grouping.getSQL().indexOf(sql.getSQL()) < 0) { + if (!_grouping.isEmpty()) + _grouping.append(", "); + _grouping.append(sql.getSQL()); + } } public void groupBy(String sql) { @@ -1386,9 +1389,12 @@ getJoins(joins, true); if (_grouping == null) _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - _grouping.append(sql); + + if (_grouping.getSQL().indexOf(sql) < 0) { + if (!_grouping.isEmpty()) + _grouping.append(", "); + _grouping.append(sql); + } } public void groupBy(Column col) { @@ -1398,11 +1404,13 @@ public void groupBy(Column col, Joins joins) { if (_grouping == null) _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); PathJoins pj = getJoins(joins, true); - _grouping.append(getColumnAlias(col, pj)); + if (_grouping.getSQL().indexOf(getColumnAlias(col, pj)) < 0) { + if (!_grouping.isEmpty()) + _grouping.append(", "); + _grouping.append(getColumnAlias(col, pj)); + } } public void groupBy(Column[] cols) { @@ -1412,14 +1420,14 @@ public void groupBy(Column[] cols, Joins joins) { if (_grouping == null) _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); PathJoins pj = getJoins(joins, true); for (int i = 0; i < cols.length; i++) { - if (i > 0) - _grouping.append(", "); - _grouping.append(getColumnAlias(cols[i], pj)); + if (_grouping.getSQL().indexOf(getColumnAlias(cols[i], pj)) < 0) { + if (!_grouping.isEmpty()) + _grouping.append(", "); + _grouping.append(getColumnAlias(cols[i], pj)); + } } }