43.Merchant - 找到最有价值的顾客
Last updated
Was this helpful?
Last updated
Was this helpful?
营销团队想发优惠券邮件给10位忠实的客户.
基于所花费的总金额列出前十大消费者.
SELECT customer.firstName,
customer.lastName,
customer.emailAddress,
SUM(items.count) purchaseCount,
ROUND(SUM(product.unitPrice * items.count)) totalSpent
FROM purchases UNNEST purchases.lineItems AS items
JOIN product ON KEYS items.product
JOIN customer ON KEYS purchases.customerId
GROUP BY customer
ORDER BY totalSpent DESC LIMIT 10
结果:
{
"results": [
{
"emailAddress": "mabel_oberbrunner@mueller.net",
"firstName": "Vernon",
"lastName": "Satterfield",
"purchaseCount": 140,
"totalSpent": 27847
},
{
"emailAddress": "opal@gaylordpouros.info",
"firstName": "Nikolas",
"lastName": "Zulauf",
"purchaseCount": 185,
"totalSpent": 27004
},
{
"emailAddress": "fabian.west@handmoen.org",
"firstName": "Verlie",
"lastName": "Fahey",
"purchaseCount": 112,
"totalSpent": 26234
},
{
"emailAddress": "helena@waltercummings.org",
"firstName": "Cary",
"lastName": "Jerde",
"purchaseCount": 125,
"totalSpent": 26035
},
{
"emailAddress": "lia@kohler.biz",
"firstName": "Carolanne",
"lastName": "Hegmann",
"purchaseCount": 133,
"totalSpent": 24875
},
{
"emailAddress": "benedict@hirtheborer.net",
"firstName": "Coralie",
"lastName": "O'Kon",
"purchaseCount": 125,
"totalSpent": 23348
},
{
"emailAddress": "jolie@beer.info",
"firstName": "Madison",
"lastName": "Klocko",
"purchaseCount": 151,
"totalSpent": 21687
},
{
"emailAddress": "tyrese@auersimonis.biz",
"firstName": "Luna",
"lastName": "Rodriguez",
"purchaseCount": 166,
"totalSpent": 21564
},
{
"emailAddress": "jonatan_armstrong@cruickshank.info",
"firstName": "Rossie",
"lastName": "Padberg",
"purchaseCount": 95,
"totalSpent": 21318
},
{
"emailAddress": "tess@bergnaum.name",
"firstName": "Jeremie",
"lastName": "Runolfsson",
"purchaseCount": 182,
"totalSpent": 21221
}
]
}