4

Let's assume I have three documents in MarkLogic as below,

 <employee>
    <name>a</name>
    <age>10</age>
</employee>

<employee>
    <name>b</name>
    <age>10</age>
</employee>

<employee>
    <name>c</name>
    <age>10</age>
</employee>

Documents uris - /employee/a, /employee/b, /employee/c respectively. I have created path range index for "/employee/age" as int scalar stype.

My Requirement is - Get the Sum of all the values of path range index "/employee/age".

I tried - sum(cts:values(cts:path-reference("/employee/age"))) but it is returning 10 as output sum here I am expecting 30 as output sum.

What will be the solution for the above problem?

DevNinja
  • 1,459
  • 7
  • 10

1 Answers1

5

The cts:values() function returns the list of distinct values present. It returned a single 10 value so your sum was 10.

Try cts:sum-aggregate() which is appropriate for this use case and takes frequency into consideration. Docs can be found at http://docs.marklogic.com/cts:sum-aggregate.

hunterhacker
  • 6,378
  • 1
  • 14
  • 11