mysql 查日期在在5.13-5.18时,订单量1-2,2-3,3-5的客户数

查日期在在5.13-5.18时,订单量1-2,2-3,3-5的客户数

一个在接单时看到的题目

下面是数据库脚本

create table flash(S varchar(10),id varchar(10),day date NOT NULL);
insert into flash values('PN0011' ,  'cc001' , '2020-05-12');
insert into flash values('PN0012' ,  'cc001' , '2020-05-13');
insert into flash values('PN0013' ,  'cc001' , '2020-05-14');
insert into flash values('PN0014' ,  'cc001' , '2020-05-14');
insert into flash values('PN0015' ,  'cc002' , '2020-05-15');
insert into flash values('PN0016' ,  'cc002' , '2020-05-16');
insert into flash values('PN0017' ,  'cc003' , '2020-05-15');
insert into flash values('PN0018' ,  'cc004' , '2020-05-16');
insert into flash values('PN0024' ,  'cc004' , '2020-05-16');
insert into flash values('PN0019' ,  'cc005' , '2020-05-16');
insert into flash values('PN0020' ,  'cc005' , '2020-05-17');
insert into flash values('PN0021' ,  'cc005' , '2020-05-17');
insert into flash values('PN0022' ,  'cc005' , '2020-05-18');
insert into flash values('PN0023' ,  'cc005' , '2020-05-18');

解题sql语句

SELECT count(t_1_2.S) `1-2客户数` from 
	(SELECT t.*,count(*) f_count from 
		flash t 
			where t.`day`>"2020-05-13" AND t.`day`<"2020-05-18" 
			GROUP BY t.id HAVING f_count >=1 and f_count<=2
	) t_1_2;

SELECT count(t_2_3.S) `2-3客户数` from 
	(SELECT t.*,count(*) f_count from 
		flash t 
			where t.`day`>"2020-05-13" AND t.`day`<"2020-05-18" 
			GROUP BY t.id HAVING f_count >=2 and f_count<=3
	) t_2_3;
	
SELECT count(t_3_5.S) `3-5客户数` from 
	(SELECT t.*,count(*) f_count from 
		flash t 
			where t.`day`>"2020-05-13" AND t.`day`<"2020-05-18" 
			GROUP BY t.id HAVING f_count >=3 and f_count<=5
	) t_3_5;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tomshidi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值