let $subbrk := (collection("DATABSE")/SUBCUSTBRK[AccNo[@NO = (12345)]][DOB >= '2019-10-01' and DOB <= '2019-10-31']) let $detailUsageTxn := $subbrk/DETAIL/TRANSACTION[@Usage = 'usage'] let $DistCallTypes := distinct-values($detailUsageTxn/SUB_SECTION/@Type) let $allmonths := distinct-values($subbrk/Month) let $rowcnt := count($detailUsageTxn/SUB_SECTION) let $months := $allmonths let $opttype := 'NumVal' let $durop := 'hh:mm:ss' return ( if($DistCallTypes!='')then ( for $month in $months return { $month }{ if($opttype = 'NumVal') then for $ct in $DistCallTypes let $ctCnt := sum($detailUsageTxn/SUB_SECTION[data(@Type) = data($ct) ]/@Calls) return element { replace(concat('_',data($ct)),'(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\)| |')', '_') } {xs:decimal($ctCnt)} } ) else() )