难写的SQL——case when 特征函数
case when 是特征函数,简单介绍见 https://www.jianshu.com/p/cbb05073931f
今天做了一个sql题,用到了case when函数,这个函数在做数据统计的时候经常用来解决条件太复杂的问题。
题干如下
# 表结构如下
CREATE TABLE `t_received` (
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`c_customer_id` varchar(15) DEFAULT NULL
PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 造数据
INSERT INTO `t_received` ( `c_customer_id` ) VALUES ( 'CC001' );
INSERT INTO `t_received` ( `c_customer_id` ) VALUES ( 'CC001' );
INSERT INTO `t_received` ( `c_customer_id` ) VALUES ( 'CC001' );
INSERT INTO `t_received` ( `c_customer_id` ) VALUES ( 'CC002' );
INSERT INTO `t_received` ( `c_customer_id` ) VALUES ( 'CC002