《SQL学习指南》

第9章

 1 SELECT groups.name, COUNT(*) num_customers
 2 FROM (
 3 SELECT SUM(a.avail_balance) cust_balance
 4 FROM account a INNER JOIN product p
 5 on a.product_cd = p.product_cd
 6 WHERE p.product_type_cd = 'account'
 7 GROUP BY a.cust_id ) cust_rollup
 8 INNER JOIN
 9 (SELECT 'small fry' name, 0 low_limit, 4999.00 high_limit
10 UNION ALL
11 SELECT 'Average Joes' name, 5000 low_limit, 9999.99 high_limit
12 UNION ALL
13 SELECT 'heavy hitters' name, 10000 low_limit, 999999.99 high_limit
14  ) groups
15 ON cust_rollup.cust_balance
16 BETWEEN groups.low_limit AND groups.high_limit
17 GROUP BY groups.name

 

上一篇:2020-11-29


下一篇:MySQL 增删查改