Default value for aggregate functions and sorting

Hi all, is there a way to do a default value for an aggregate function, e.g. 0 for a sum(children.points) or 0.

Query:

query {
  topic(func: eq(id, "1234")) {
    id
    article(orderdesc: points, orderasc: createdAt) {
      id
      points: sum(val(p)) # or 0
      votes {
        id
        p as points
        createdAt
      }
    }
  }
}

If article has no vote nodes than point is empty and sort doesn’t work. I’d like it to be 0 so sort can put it below others

Any tips?

if it is zero, it should be in the last of the result

Hi Jerry, my point is that it should be zero but it’s not. If there are no votes (no child nodes) than instead of zero, the points variable is “missing”. This causes the sort to order incorrectly.

Zak

if the point is missing, what’s the result’s order

Try adding a Kind https://docs.dgraph.io/howto/#giving-nodes-a-type

Like:

has (missing)

PS. But you should create an automatic process that adds zero to the new user. A simple registration mutation already with the added “zero” would solve this. Do not create blank fields where you need sorting.