MySQL条件判断处理

MySQL条件判断处理

MySQL条件判断处理

一、假如我想把salesperson 分成 5组,计算每个销售分组的业绩

首先先将销售分组

 SELECT *,
CASE
WHEN salesperson IN ("丁test","何test","刘test","叶test","夏test") THEN "销售一组"
WHEN salesperson IN ("姚test","崔test","张test","徐test","曹test") THEN "销售二组"
WHEN salesperson IN ("李test","杨test","王test","石test","粟test") THEN "销售三组"
WHEN salesperson IN ("胡test","薛test","谈test","邓test","邱test") THEN "销售四组"
WHEN salesperson IN ("韩test","马test","魏test") THEN "销售五组"
ELSE NULL END AS 销售分组
FROM test_a03order AS a

MySQL条件判断处理函数_20160925-LMLPHP

根据生成的销售分组字段进行聚合

 SELECT 销售分组,SUM(pay_money) AS 业绩
FROM (
SELECT *,
CASE
WHEN salesperson IN ("丁test","何test","刘test","叶test","夏test") THEN "销售一组"
WHEN salesperson IN ("姚test","崔test","张test","徐test","曹test") THEN "销售二组"
WHEN salesperson IN ("李test","杨test","王test","石test","粟test") THEN "销售三组"
WHEN salesperson IN ("胡test","薛test","谈test","邓test","邱test") THEN "销售四组"
WHEN salesperson IN ("韩test","马test","魏test") THEN "销售五组"
ELSE NULL END AS 销售分组
FROM test_a03order AS a
) AS b
GROUP BY 销售分组

MySQL条件判断处理函数_20160925-LMLPHP

二、我想对城市业绩进行分组  金额500以下的分为一组,金额500到1000的一组 1000到4000的一组 4000以上的一组

 SELECT city,SUM(pay_money) AS 业绩,
CASE WHEN SUM(pay_money)<=500 THEN "(0000,0500]"
WHEN SUM(pay_money)<=1000 THEN "(0500,1000]"
WHEN SUM(pay_money)<=4000 THEN "(1000,4000]"
ELSE "(4000以上)" END AS "业绩区间"
FROM test_a03order AS a
GROUP BY city
ORDER BY SUM(pay_money) DESC
05-04 04:16