Index: ResultSetForSetProviderOperation.java
===================================================================
--- ResultSetForSetProviderOperation.java	(revision 46049)
+++ ResultSetForSetProviderOperation.java	(revision 46050)
@@ -52,11 +52,13 @@
 import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
 import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
 import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER;
+import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY;
 import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE;
 import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
 import org.gvsig.fmap.geom.DataTypes;
 import org.gvsig.tools.dynobject.DynField;
 import org.gvsig.tools.evaluator.Evaluator;
+import org.gvsig.tools.util.ContainerUtils;
 
 public class ResultSetForSetProviderOperation extends AbstractConnectionOperation {
 
@@ -117,7 +119,7 @@
             List<FeatureAttributeDescriptor> columns,
             List<String> extraColumnNames
     ) {
-        double tolerance = -1; //query.getScale();
+        double tolerance = -1; //query.getScale(); 
         ExpressionBuilder expbuilder = sqlbuilder.expression();
         SelectBuilder select = sqlbuilder.select();
 
@@ -141,22 +143,48 @@
         if (query != null && query.hasConstantsAttributeNames()) {
             constantsAttributeNames = query.getConstantsAttributeNames();
         }
+        ArrayList<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>();
         for (FeatureAttributeDescriptor attr : setType) {
             if (attr.isComputed()) {
-//              if(StringUtils.isNotBlank(System.getenv("ENABLE_COMPUTED_SQL_ATTR"))) {
+//              if(StringUtils.isNotBlank(System.getenv("ENABLE_COMPUTED_SQL_ATTR"))) { 
                 if (attr.getRelationType() == DynField.RELATION_TYPE_NONE) {
                     FeatureAttributeEmulator attrEmulator = attr.getFeatureAttributeEmulator();
                     if (attrEmulator instanceof FeatureAttributeEmulatorExpression) {
                         FeatureAttributeEmulatorExpression x = (FeatureAttributeEmulatorExpression) attrEmulator;
                         Expression exp = x.getExpression();
-                        if (exp != null && !exp.isEmpty() && this.helper.supportExpression(setType, exp.getPhrase())) {
-                            Code code = exp.getCode();
-                            select.column()
-                                    .value(code.toValue(expbuilder))
-                                    .as(attr.getName());
-                            if (!extraColumnNames.contains(attr.getName())) {
-                                extraColumnNames.add(attr.getName());
+
+                        if (query != null && query.hasGroupByColumns()) {
+                            String aggregate = query.getAggregate(this.table.getTable(), attr.getName());
+                            if (this.query.isAGroupByColumn(attr.getName())) {
+                                if (!select.has_column(attr.getName())) {
+                                    select.column().value(exp.getCode().toValue()).as(attr.getName());
+                                }
+                                if (extraColumnNames != null && !extraColumnNames.contains(attr.getName())) {
+                                    extraColumnNames.add(attr.getName());
+                                }
+                            } else if (aggregate == null) {
+                                select.column().value(expbuilder.constant(null)).as(attr.getName());
+                            } else {
+                                String fn = this.query.getAggregateFunctions().get(attr.getName());
+                                ExpressionBuilder.Function aggregateExp = expbuilder.function(fn, exp.getCode().toValue());
+                                if (!select.has_column(attr.getName())) {
+                                    select.column().value(aggregateExp).as(attr.getName());
+                                }
+                                if (extraColumnNames != null && !extraColumnNames.contains(attr.getName())) {
+                                    extraColumnNames.add(attr.getName());
+                                }
                             }
+                        } else {
+                            if (exp != null && !exp.isEmpty() && this.helper.supportExpression(setType, exp.getPhrase())) {
+                                Code code = exp.getCode();
+                                select.column()
+                                        .value(code.toValue(expbuilder))
+                                        .as(attr.getName());
+                                if (extraColumnNames != null && !extraColumnNames.contains(attr.getName())) {
+                                    extraColumnNames.add(attr.getName());
+                                }
+                            }
+
                         }
                     }
                 }
@@ -209,7 +237,7 @@
                     if (!select.has_column(attr.getName())) {
                         select.column().value(aggregateExp).as(attr.getName());
                     }
-                    if (!extraColumnNames.contains(attr.getName())) {
+                    if (extraColumnNames!=null && !extraColumnNames.contains(attr.getName())) {
                         extraColumnNames.add(attr.getName());
                     }
                 }
@@ -217,22 +245,29 @@
             for (String attrName : query.getGroupByColumns()) {
                 if (allExtraColumns.get(attrName) != null) { //from setType and query
                     EditableFeatureAttributeDescriptor attr = allExtraColumns.get(attrName);
-                    select.group_by(expbuilder.column(attrName));
+                    ExpressionBuilder.Variable col = expbuilder.column(attrName);
+                    select.group_by(col);
+                    // En el groupBy no queremos que se sustituya el nombre del campo calculado
+                    // por su expresion. Se encarga el formater y lo evitamos quitandole el ftype
+                    // al value.
+                    valuesToRemoveFeatureType.add(col);
                     Expression exp = ((FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator()).getExpression();
                     if (!select.has_column(attrName)) {
                         select.column().value(exp.getCode().toValue()).as(attrName);
                     }
-                    if (!extraColumnNames.contains(attr.getName())) {
+                    if (extraColumnNames!=null && !extraColumnNames.contains(attr.getName())) {
                         extraColumnNames.add(attrName);
                     }
                 } else if (setType.get(attrName) != null && setType.getAttributeDescriptor(attrName).isComputed()) {
                     FeatureAttributeDescriptor attr = setType.getAttributeDescriptor(attrName);
-                    select.group_by(expbuilder.column(attrName));
+                    ExpressionBuilder.Variable col = expbuilder.column(attrName);
+                    select.group_by(col);
+                    valuesToRemoveFeatureType.add(col);
                     Expression exp = ((FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator()).getExpression();
                     if (!select.has_column(attrName)) {
                         select.column().value(exp.getCode().toValue()).as(attrName);
                     }
-                    if (!extraColumnNames.contains(attr.getName())) {
+                    if (extraColumnNames!=null && !extraColumnNames.contains(attr.getName())) {
                         extraColumnNames.add(attrName);
                     }
                 } else if (setType.get(attrName) == null) {
@@ -268,7 +303,7 @@
                                 select.column()
                                         .value(code.toValue(expbuilder))
                                         .as(attr.getName());
-                                if (!extraColumnNames.contains(attr.getName())) {
+                                if (extraColumnNames!=null && !extraColumnNames.contains(attr.getName())) {
                                     extraColumnNames.add(attr.getName());
                                 }
                             }
@@ -312,7 +347,7 @@
                         if (!select.has_column(attrName)) {
                             select.column().value(exp.getCode().toValue()).as(attrName);
                         }
-                        if (!extraColumnNames.contains(attrName)) {
+                        if (extraColumnNames!=null && !extraColumnNames.contains(attrName)) {
                             extraColumnNames.add(attrName);
                         }
                     } else if (setType.get(attrName) != null && setType.getAttributeDescriptor(attrName).isComputed()) {
@@ -320,7 +355,7 @@
                         if (!select.has_column(attrName)) {
                             select.column().value(exp.getCode().toValue()).as(attrName);
                         }
-                        if (!extraColumnNames.contains(attrName)) {
+                        if (extraColumnNames!=null && !extraColumnNames.contains(attrName)) {
                             extraColumnNames.add(attrName);
                         }
                     }
@@ -386,8 +421,12 @@
                 PROP_FEATURE_TYPE, this.storeType,
                 PROP_TABLE, table,
                 PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(),
-                PROP_JDBCHELPER, this.helper
+                PROP_JDBCHELPER, this.helper,
+                PROP_QUERY, this.query
         );
+        for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) {
+            value.setProperty(PROP_FEATURE_TYPE, null);
+        }
         this.helper.processSpecialFunctions(sqlbuilder, storeType, extraColumnNames);
         String sql = sqlbuilder.toString();
         return sql;
