Hello Markus,

In your query, you're grouping only by $count but also returning $incidence. When you group by $count, you're creating a sequence of unique count values each associated with a sequence of $incidences. To illustrate:

<contributors>{
for $x in $results
let $count := $x(1)
let $incidence := count($count)
group by $count
return <contributor count="{$count}">{
  for $i in $incidence
  return <incidence>{$i}</incidence>
}</contributor>
}</contributors>

Will result in:

<contributors>
  <contributor count="1">
    <incidence>1</incidence>
    <incidence>1</incidence>
    <incidence>1</incidence>
  </contributor>
  <contributor count="2">
    <incidence>1</incidence>
  </contributor>
</contributors>

So when you attempt to print out <incidence>{$incidence}</incidence>, XQuery finds a sequence (1, 1, 1) instead of the value you want, 3.

To get the count of $incidence, you could return <incidence>{count($incidence)}</incidence> instead. But since you're not using the IDs at all, placing them in a sequence of arrays first isn't necessary. You can skip that part like:

for $x in //test_data/product
let $count := count($x/contributor)
let $id := $x/id/data()
group by $count
let $incidence := count($id)
order by $incidence descending
return
<csv>
   <record>
   <contributor_count>{$count}</contributor_count>
   <incidence>{$incidence}</incidence>
   </record>
 </csv>

-Tamara

On Mon, Apr 4, 2022 at 8:30 AM Markus Elfring <Markus.Elfring@web.de> wrote:
> As I said, consider to provide an input/output sample of what you have
> and want to achieve, …

I did that a moment ago.
https://mailman.uni-konstanz.de/pipermail/basex-talk/2022-April/017045.html


A simple query can provide the following data display.

id|contributor_count
123|1
45|1
67|1
89|2


These data should be taken for another analysis.

contributor_count|incidence
1|3
2|1


This would be the expected output for such a test case.

I became curious if such a report result could be achieved also without
the generation of the first table.

Regards,
Markus


--

Tamara Marnell
Program Manager, Systems
Orbis Cascade Alliance (orbiscascade.org)
Pronouns: she/her/hers