From christian.gruen at gmail.com Tue Apr 26 03:17:04 2016 From: christian.gruen at gmail.com (=?UTF-8?Q?Christian_Gr=C3=BCn?=) Date: Tue, 26 Apr 2016 12:17:04 +0200 Subject: [xquery-talk] find most occurring incidence In-Reply-To: References: Message-ID: Hi Leo, here?s one more solution. It is not much shorter than yours, but may be a bit more readable: let $cols := for $colors in //color group by $value := $colors/data() return let $max := max($cols/@count) return $cols[@count = $max]/@value/data() I think that in every case the values need to be accessed at least twice, because we?ll first need to find out what is the actual maximum count, and then need to look for all values matching that maximum. Hope this helps, Christian On Tue, Apr 26, 2016 at 12:02 PM, Leo Studer wrote: > Hello Cracks ;-) > > I need to find the most occurring incidence, lets say the color that occurs > most out of > > > red > green > blue > red > green > green > red > blue > > > > > Here my first solution > > (for $color in distinct-values(//color) > order by count(//color[. eq $color]) descending > return $color)[1] > > Problem: red and green have both 3 occurrences. > > This works but looks horrible > > for $max in max(for $color in distinct-values(//color) return > count(//color[. eq $color])), > $color in distinct-values(//color) > return > if (count(//color[. eq $color]) eq $max) then $color else () > > > Any better suggestions? > > Thanks in advance > Leo > > _______________________________________________ > talk at x-query.com > http://x-query.com/mailman/listinfo/talk From christian.gruen at gmail.com Tue Apr 26 03:35:34 2016 From: christian.gruen at gmail.com (=?UTF-8?Q?Christian_Gr=C3=BCn?=) Date: Tue, 26 Apr 2016 12:35:34 +0200 Subject: [xquery-talk] find most occurring incidence In-Reply-To: References: Message-ID: And two another solutions, taking advantage of maps: The more readable one: let $map := map:merge( for $color in //color group by $value := $color return map { $value: count($color) } ) let $max := max($map?*) return map:for-each($map, function($color, $value) { if($value eq $max) then $color else () }) The more cryptical (and not that efficient) one: let $m := map:merge( for $v in distinct-values(//color) return map { $v: count(//color[. eq $v]) } ) return map:keys($m)[$m(.) = max($m?*)] On Tue, Apr 26, 2016 at 12:17 PM, Christian Gr?n wrote: > Hi Leo, > > here?s one more solution. It is not much shorter than yours, but may > be a bit more readable: > > let $cols := > for $colors in //color > group by $value := $colors/data() > return > let $max := max($cols/@count) > return $cols[@count = $max]/@value/data() > > I think that in every case the values need to be accessed at least > twice, because we?ll first need to find out what is the actual maximum > count, and then need to look for all values matching that maximum. > > Hope this helps, > Christian > > > > On Tue, Apr 26, 2016 at 12:02 PM, Leo Studer wrote: >> Hello Cracks ;-) >> >> I need to find the most occurring incidence, lets say the color that occurs >> most out of >> >> >> red >> green >> blue >> red >> green >> green >> red >> blue >> >> >> >> >> Here my first solution >> >> (for $color in distinct-values(//color) >> order by count(//color[. eq $color]) descending >> return $color)[1] >> >> Problem: red and green have both 3 occurrences. >> >> This works but looks horrible >> >> for $max in max(for $color in distinct-values(//color) return >> count(//color[. eq $color])), >> $color in distinct-values(//color) >> return >> if (count(//color[. eq $color]) eq $max) then $color else () >> >> >> Any better suggestions? >> >> Thanks in advance >> Leo >> >> _______________________________________________ >> talk at x-query.com >> http://x-query.com/mailman/listinfo/talk