Expression with two groups

Use a combination of the two fields like concat: "id" || "function" for the group_by part of the expression:

sum("area","id"||"function")

There is also a possibility using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is a point layer called "test" with its attribute table, see image below.

input

With the following query, it is possible to calculate the sum of the features only when these have the same "id" and the same "function", as well as getting rid of duplicates.

SELECT SUM(value) AS sumvalue,
       COUNT(*) AS numgrouped,
       "id"||"function" AS grouped,
       geometry AS geom
FROM "test"
GROUP BY "id", "function"

The output point layer with a new attribute table will look like

result

Last but not the least, keep in mind that:

  • the number of features in the final output will be less (rarely the same) than in the original shapefile
  • grouping values of features with neglecting their geometries is not the same as the grouping values of features with preserving their geometries