include "normalize"; [.accounts[] | account_rows | select(any(.promotions[]; . == $promo))] | group_by([.accountId, .currency]) | map({ accountId: .[0].accountId, currency: .[0].currency, orderIds: (map(.orderId) | unique), orderCount: (map(.orderId) | unique | length), capturedOrderCents: (unique_by(.orderId) | map(.orderTotalCents) | add), retainedPromotedListCents: (map(.netListCents) | add), netPromotedQty: (map(.netQty) | add), skus: (group_by(.sku) | map({sku: .[0].sku, netQty: (map(.netQty) | add)})) })