0

There is a polygon layer "Union23" that was created with 'Union' tool.

Polygon_Layer

Layer's attribute table includes the following values.

AT_values

My effort is simply to aggregate those polygons by "FLAECHEID" field with the sum and count of a field "In_Value1" via Virtual Layer. So I am using the expression

SELECT *, SUM(In_Value1), COUNT(In_Value1)
FROM Union23
GROUP BY "FLAECHEID"

However, I am getting really weird output and I do not understand why I am losing a part of geometry on the way? Any suggestions?

VL_Output


UPDATE

When I use the solution offered by @JGH I am somehow getting better output but still with small issues. Is it related to the original geometry?

VL_output_2


References:

1

When using a mix of an aggregate function and a regular row, SQLite will return a random row for the not-aggregated field (see doc, point 3). To overcome this, don't use * but properly aggregated fields, including on the geometry

SELECT ST_UNION(geometry), SUM(In_Value1), COUNT(In_Value1)
FROM Union23
GROUP BY "FLAECHEID"
  • So to say, If I want to maintain all of my fields I will need to perform it manually instead of star-symbol *, am I right? – Taras Mar 12 at 12:03
  • Yes. with min/max etc, unless they all have the same value and a random row is OK – JGH Mar 12 at 12:05
  • You could also do a join on Flaecheid between the spatial layer and the newly computed stat table - that would not contain any geometry - – JGH Mar 12 at 12:14
  • yeap, I think this will be the best option via Join. thank you – Taras Mar 12 at 12:18
  • 2
    I often find that unexpected results can be traced to bad geometry, particularly with shapefiles. Consider validating both the input and output layer's geometry with Check Geometry. You can also Repair. Then – Stu Smith Mar 13 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.