Prism · Real world
A summary of orders
A small dashboard from raw orders: revenue, the average and the largest order, how many there were, and who ordered.
{
orders: [
{ id: 'o1', customer: 'Ada', amount: 120 },
{ id: 'o2', customer: 'Grace', amount: 85 },
{ id: 'o3', customer: 'Ada', amount: 200 },
{ id: 'o4', customer: 'Linus', amount: 45 },
{ id: 'o5', customer: 'Grace', amount: 175 },
{ id: 'o6', customer: 'Ada', amount: 60 },
],
}{
totalRevenue: {
$sum: {
over: { $pluck: { over: { $ref: '$.orders' }, key: 'amount' } },
},
},
avgOrder: {
$avg: {
over: { $pluck: { over: { $ref: '$.orders' }, key: 'amount' } },
},
},
maxOrder: {
$max: {
over: { $pluck: { over: { $ref: '$.orders' }, key: 'amount' } },
},
},
orderCount: { $count: { over: { $ref: '$.orders' } } },
uniqueCustomers: {
$unique: {
$pluck: { over: { $ref: '$.orders' }, key: 'customer' },
},
},
}{
totalRevenue: 685,
avgOrder: 114.16666666666667,
maxOrder: 200,
orderCount: 6,
uniqueCustomers: ['Ada', 'Grace', 'Linus'],
}Runs in this page on the installed package. The input and the expected output are defined by the example.