Dear Alexander, "the best way out is always through", the American poet Robert Frost wrote, and this certainly applies to understanding the semantics of FLWOR expressions in general and the group by clause in particular. When you agree to go through the abstract principle behind FLWOR expressions, you get out of confusion caused by any concrete scenarios. First the terms: a tuple is a set of variable bindings; a tuple stream is a sequence of tuples. Now the FLWOR principle: (1) The first clause generates a stream of tuples, according to the rules applying to the particular clause; e.g. let creates an initial tuple stream consisting of a a single tuple with a single variable; for creates a stream containing one tuple per item in the for expression value, a tuple containing a single variable bound to that item; window creates a sequence of tuples which may have up to nine variables ...)(2) Every later clause preceding the return clause maps the input tuple stream to an output tuple stream (which is the input tuple stream of the subsequent clause); this mapping is again performed according to rules specific for the kind of clause(3) The group by clause rules are:(a) Group the input tuple stream by the grouping variable(b) For each set of input tuples with the same value of the grouping variable create one output tuple, with the following content:(b1) A variable with the name of the grouping variable, bound to the current grouping key (in your case a $key variable)(b2) For each other variable encountered in the input tuples, create a variable with that name, bound to the concatenated sequence of values which the variable has in the individual tuples of the group (in your case $input, $map, $result, $value variables) (b2) is the tricky part. Your expectation that the grouping is somehow related to the preceding for clause is not correct - it is the complete tuple stream which is mapped to one tuple per subset of the tuple stream with an equal value of the grouping key. In your case, the input tuples have the following variables: $input , $map, $result, $key, $value. The value of $input and $map is in all tuples the same, so the value in the output tuples of the group by clause contains as many repetitions of the input value as there are members in the group (tuples with the current grouping key). The output tuple created for grouping key value "b" represents a group of two input tuples, so $input is bound to a sequence of two equal root elements, and $map is bound to a sequence of two equal maps. This is the reason why Martin's advice helps: as the let clauses creating $input and $map are followed by a return, these variables are, while available as part of the evaluation context, not any more part of the tuple stream whose build-up starts afresh with the for clause nested in the return clause. The group by clause receives tuples with the following variables: $result, $key, $value. The problem is removed, as the input tuples processed by the group by clause simply do not any more contain $input and $map variables. Kind regardsHans-Jürgen Am Freitag, 6. Oktober 2023 um 09:16:18 MESZ hat Alexander Krasnogolowy <alexkra...@hotmail.com> Folgendes geschrieben: #yiv1934847823 P {margin-top:0;margin-bottom:0;}Given the following XQuery:let $input := <root> <item><key>a</key><value>1</value></item> <item><key>b</key><value>2</value></item> <item><key>b</key><value>3</value></item> </root> let $map := map:merge(( map:entry(1, "A"), map:entry(2, "B"), map:entry(3, "C"))) for $result in $input/*let $key := $result/key/text()let $value := $result/value/text()group by $keyreturn map:get($map, $key)
When trying to execute that query, I get the error "Item expected, sequence found: (map {1:"A",2:"B",3:"C"}, map {1:"A",2:"B",3:"C"}).But why does it group the map? It isn't part of that for-loop but rather some kind of global variable. Or do I misunderstand the "group by" clause?I only expect the $value to be grouped which results in a sequence. Thanks,Alex