MySQL条件判断处理函数_20160925
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL条件判断处理函数_20160925相关的知识,希望对你有一定的参考价值。
mysql条件判断处理
一、假如我想把salesperson 分成 5组,计算每个销售分组的业绩
首先先将销售分组
1 SELECT *, 2 CASE 3 WHEN salesperson IN ("丁test","何test","刘test","叶test","夏test") THEN "销售一组" 4 WHEN salesperson IN ("姚test","崔test","张test","徐test","曹test") THEN "销售二组" 5 WHEN salesperson IN ("李test","杨test","王test","石test","粟test") THEN "销售三组" 6 WHEN salesperson IN ("胡test","薛test","谈test","邓test","邱test") THEN "销售四组" 7 WHEN salesperson IN ("韩test","马test","魏test") THEN "销售五组" 8 ELSE NULL END AS 销售分组 9 FROM test_a03order AS a
根据生成的销售分组字段进行聚合
1 SELECT 销售分组,SUM(pay_money) AS 业绩 2 FROM ( 3 SELECT *, 4 CASE 5 WHEN salesperson IN ("丁test","何test","刘test","叶test","夏test") THEN "销售一组" 6 WHEN salesperson IN ("姚test","崔test","张test","徐test","曹test") THEN "销售二组" 7 WHEN salesperson IN ("李test","杨test","王test","石test","粟test") THEN "销售三组" 8 WHEN salesperson IN ("胡test","薛test","谈test","邓test","邱test") THEN "销售四组" 9 WHEN salesperson IN ("韩test","马test","魏test") THEN "销售五组" 10 ELSE NULL END AS 销售分组 11 FROM test_a03order AS a 12 ) AS b 13 GROUP BY 销售分组
二、我想对城市业绩进行分组 金额500以下的分为一组,金额500到1000的一组 1000到4000的一组 4000以上的一组
1 SELECT city,SUM(pay_money) AS 业绩, 2 CASE WHEN SUM(pay_money)<=500 THEN "(0,500]" 3 WHEN SUM(pay_money)>500 AND SUM(pay_money)<=1000 THEN "(500,1000]" 4 WHEN SUM(pay_money)>1000 AND SUM(pay_money)<=4000 THEN "(1000,4000]" 5 ELSE "(4000,3000+)" END AS "业绩区间" 6 FROM test_a03order AS a 7 GROUP BY city 8 ORDER BY SUM(pay_money) DESC
以上是关于MySQL条件判断处理函数_20160925的主要内容,如果未能解决你的问题,请参考以下文章