include "normalize"; include "reward-functions"; [.accounts[] | account_rows] as $rows | ($rows | reward_buckets($rules[0])) as $buckets | { byAccount: ($buckets | group_by(.accountId) | map({ accountId: .[0].accountId, netQty: (map(.netQty) | add), points: (map(.points) | add), skus: map({sku, netQty, basePoints, multiplier, points, missingRate}) })), bySku: ($buckets | group_by(.sku) | map({ sku: .[0].sku, netQty: (map(.netQty) | add), points: (map(.points) | add) })), totalPoints: ($buckets | map(.points) | add // 0), missingRates: ([$buckets[] | select(.missingRate) | .sku] | unique) }