Christian,

This function never ends with Basex 8.0.
"CRM" database has records sorted by PRODUCT_ID. In tumbling window, I collect all records with same PRODUCT_ID and process them,

declare %updating function local:addCRMDataIntoProducts ()
{

     let $allProds := fn:doc("AccumulatedProducts")
     let $segments := ("A","B","C","D","E","F","G"}
     let $productsMap :=
            map:new(
              for $rec in $allProds//doc
                  let $prodIDField := $rec/field[@name eq "ProductID"]
                  let $pid := $prodIDField/text()
                    return
                          map:entry($pid,$prodIDField))

     for tumbling window $prodGroup in fn:doc("CRM")//record
          start $first next $second when $first/PRODUCT_ID eq $second/PRODUCT_ID
          end $last next $beyond when $last/PRODUCT_ID ne $beyond/PRODUCT_ID

          let $pid := $prodGroup[1]/PRODUCT_ID
          return
            let $prodEntry := map:get($productsMap,$pid)/..
            return
              if (fn:empty($prodEntry))   then  ()
              else
                  let $totalAmount := sum($prodGroup//AMOUNT/text())
                  let $totalOrderCount := sum($prodGroup//ORDER_COUNT)
                  let $customers :=
                    for $cid in $prodGroup/CUSTOMER_ID/text()
                      return
                        <field name="CustomersPurchased">{$cid}</field>
                  let $sumSolrFields := (<field name="Amount">{$totalAmount}</field>,
                                       <field name="OrderCount">{$totalOrderCount}</field>,
                                        $customers)
                  let $segData :=
                    for $segGroup in $prodGroup
                      let  $sid := $segGroup/SON_SEGMENT/text()
                      let $sid := fn:concat("10",fn:index-of($segments,$sid))
                      group by $sid
                      return
                        let $segAmount := sum($segGroup//AMOUNT/text())
                        let $segOrderCount := sum($segGroup//ORDER_COUNT)
                        let $segFields := (<field name="SegAmount_{$sid}">{$segAmount}</field>,
                                          <field name="SegOrderCount_{$sid}">{$segOrderCount}</field>)
                        return $segFields

                 return
                   insert nodes ($segData,$sumSolrFields) as last into $prodEntry
}

On Sun, Jan 11, 2015 at 6:33 AM, Christian Grün <christian.gruen@gmail.com> wrote:
Hi Erol,

> I am using Basex 7.9 and my script is running well.
> I can not use 8.0 because I have tested script with it , it can not finish.

Do you mean that the script does not work with the 8.0 snapshot? Do
you have more information?

> How difficult to back port sql changes to Basex 7.9 branch?

It's not difficult per se, but it mainly takes time (we haven't added
any features to 7.9 so far).

Best,
Christian