Bean & Bark: answer a business question
You found Dana in one cheap read. This one's different: why did the West region's revenue fall 12% last month? There is no partition key for "revenue" — this isn't a lookup in the table, it's a question about it. That's what SUM and GROUP BY are for, and it's where you meet the honest cost of asking DynamoDB an analytical question.
- Fold thousands of rows into an answer with
SUMandGROUP BY - Understand that DynamoDB has no
GROUP BY— the studio reads the matched items and folds them client-side, and every item read is billed - Follow a total to its cause: what changed, then why it changed
You can't look this up — you have to fold it
A key gets you one partition. This question spans the whole West region across months — there's nothing to key on. DynamoDB itself has no GROUP BY, so DynoStudio does the fold for you: it reads the matching orders and adds them up client-side, one row per month. Useful — but be clear-eyed about the strip. To total the West's orders it must first Scan for them, and everything it reads is billed.
-- there is no partition called "revenue" — you fold the tableSELECT month, SUM(TotalCents) / 100 AS revenueFROM "BeanAndBark"WHERE Region = 'West' AND begins_with(sk, 'ORDER#')GROUP BY monthORDER BY month There's the shape of it: a February peak, then a slide — June is down about 12% from that top. But a total only tells you what happened, not why. So you ask a second question, and this is the move that earns your morning: instead of grouping orders by month, group subscription cancellations by month.
SUM/GROUP BYanswer questions about the table (totals, trends), not lookups in it — there's no key for "revenue".- DynamoDB has no
GROUP BY: the studio reads every matched item and folds client-side, so a cross-cutting aggregate is a Scan you pay for in full. - A total tells you what changed; a second grouping (churn by month) tells you why.
SUM(...) GROUP BY month over the West region cost more than looking up one customer's orders?GROUP BY actually execute?RoastLevel instead of month.month for RoastLevel in both the SELECT and the GROUP BY.Show solution
RoastLevel — the decaf row is the one that fell, which is the thread that leads to the churned subscriptions.SELECT RoastLevel, SUM(TotalCents) / 100 AS revenueFROM "BeanAndBark"WHERE Region = 'West' AND begins_with(sk, 'ORDER#')GROUP BY RoastLevel begins_with(sk, 'SUB#')) with Status = 'cancelled'. Count them with COUNT(*), grouped by month.Show solution
COUNT(*) ... GROUP BY month over cancelled West subscriptions puts a hard number on the March spike — the evidence behind the revenue drop.SELECT month, COUNT(*) AS cancellationsFROM "BeanAndBark"WHERE Region = 'West' AND begins_with(sk, 'SUB#') AND Status = 'cancelled'GROUP BY monthORDER BY month