43.Merchant - 找到最有价值的顾客

营销团队想发优惠券邮件给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

结果:

Last updated

Was this helpful?