我們擁有下面這個(gè) "Orders" 表:
現(xiàn)在柜裸,我們希望查找每個(gè)客戶所有訂單的總金額。怎么做粱锐?
分析思路
1疙挺、找出每個(gè)客戶的每筆訂單并根據(jù)客戶進(jìn)行分組。
2怜浅、把每個(gè)用戶的所有訂單金額相加铐然,統(tǒng)計(jì)出每個(gè)用戶訂單總金額
用sql實(shí)現(xiàn)(GROUP BY)
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
統(tǒng)計(jì)訂單金額大于2000的用戶(having)
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer having SUM(OrderPrice) >2000