1.解决思路:
用自连接, 假定表名为"交易表"
select ab.客户号 from ( select a.客户号,a.时间, count(*) from 交易表 a inner join 交易表 b on a.客户号=b.客户号 where b.时间>=a.时间 and b.时间 < a.时间 + 3分钟 group by a.客户号,a.时间 having count(*)>10 ) as ab group by ab.客户号
2.深度思考:
(1)数据量较大时,可能会出现性能问题
(2)如果因数据量大,拆成按天统计,考虑是否需要关注零点时刻统计可能不准确的问题。
3.验证数据准备:
create multiset table trade_evt ( tx_time timestamp(6) ,customer_id varchar(100) )no primary index; insert into trade_evt values('2019-05-22 14:40:01','000'); insert into trade_evt values('2019-05-22 14:40:02','000'); insert into trade_evt values('2019-05-22 14:40:03','000'); insert into trade_evt values('2019-05-22 14:40:04','000'); insert into trade_evt values('2019-05-22 14:40:05','000'); insert into trade_evt values('2019-05-22 14:41:01','000'); insert into trade_evt values('2019-05-22 14:41:02','000'); insert into trade_evt values('2019-05-22 14:41:03','000'); insert into trade_evt values('2019-05-22 14:41:04','000'); insert into trade_evt values('2019-05-22 14:41:05','000'); insert into trade_evt values('2019-05-22 14:42:01','000'); insert into trade_evt values('2019-05-22 14:42:02','000'); insert into trade_evt values('2019-05-22 14:42:03','000'); insert into trade_evt values('2019-05-22 14:42:04','000'); insert into trade_evt values('2019-05-22 14:42:05','000'); insert into trade_evt values('2019-05-22 14:43:01','000'); insert into trade_evt values('2019-05-22 14:43:02','000'); insert into trade_evt values('2019-05-22 14:43:03','000'); insert into trade_evt values('2019-05-22 14:43:04','000'); insert into trade_evt values('2019-05-22 14:43:05','000'); insert into trade_evt values('2019-05-22 14:40:01','001'); insert into trade_evt values('2019-05-22 14:40:02','001'); insert into trade_evt values('2019-05-22 14:41:01','001'); insert into trade_evt values('2019-05-22 14:41:02','001'); insert into trade_evt values('2019-05-22 14:42:01','001'); insert into trade_evt values('2019-05-22 14:42:02','001'); insert into trade_evt values('2019-05-22 14:43:01','001'); insert into trade_evt values('2019-05-22 14:43:02','001'); insert into trade_evt values('2019-05-22 14:44:01','001'); insert into trade_evt values('2019-05-22 14:45:01','001'); insert into trade_evt values('2019-05-22 14:46:01','001');
4.结果SQL:查询3分钟内交易超过10次的记录(客户号去重即可得到目标结果)
select a.customer_id ,a.tx_time as start_time ,a.tx_time + interval '3' minute as end_time --3分钟 ,count(*) times from trade_evt a inner join trade_evt b on a.customer_id=b.customer_id where b.tx_time>=a.tx_time and b.tx_time<(a.tx_time + interval '3' minute) --3分钟内(含) group by a.customer_id,a.tx_time having times >=10 --交易次数超过10次 order by 1,2