♣题目部分 查询各个用户最长的连续登陆天数?
♣答案部分
row_number() over() 解说例一: row_number() over() :简单的说row_number()从1开始,为每一条分组记录返回一个数字。例如首先在在线数据库中创建表,插入上图左边源表数据CREATE TABLE IF NOT EXISTS `loadrecord` ( `uid` int(6) unsigned NOT NULL, `loadtime` varchar(200) NOT NULL, PRIMARY KEY (`uid`,`loadtime`)) DEFAULT CHARSET=utf8;INSERT INTO `loadrecord` (`uid`, `loadtime`) VALUES ('201', '2017/1/1'), ('201', '2017/1/2'), ('202', '2017/1/2'), ('202', '2017/1/3'), ('203', '2017/1/3'), ('201', '2017/1/4'), ('202', '2017/1/4'), ('201', '2017/1/5'), ('202', '2017/1/5'), ('201', '2017/1/6'), ('203', '2017/1/6'), ('203', '2017/1/7'); 其中,select uid,(row_number() over(partition by uid order by uid ) ) AS row_number_uid from loadrecord;返回为升序以后的每条uid记录返回一个序号:
row_number() over()分组排序功能:在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where 、group by、 order by 的执行。
例二:
create table TEST_ROW_NUMBER_OVER( id varchar(10) not null, name varchar(10) null, age varchar(10) null, salary int null);select * from TEST_ROW_NUMBER_OVER t; insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a',10,8000);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a2',11,6500);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b',12,13000);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b2',13,4500);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c',14,3000);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c2',15,20000);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(4,'d',16,30000);insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(5,'d2',17,1800);
对查询结果进行排序(无分组):
select id,name,age,salary,row_number()over(order by salary desc) rnfrom TEST_ROW_NUMBER_OVER t
根据id分组排序:
select id,name,age,salary,row_number()over(partition by id order by salary desc) `rank`from TEST_ROW_NUMBER_OVER t;
找出每一组中序号为一的数据?
select * from (select id,name,age,salary,row_number()over(partition by id order by salary desc) `rank`from TEST_ROW_NUMBER_OVER ) t where `rank` <2;
排序找出年龄在13岁到16岁数据,按salary排序:
select id,name,age,salary,row_number()over(order by salary desc) `rank`from TEST_ROW_NUMBER_OVER t where age between '13' and '16';
#结果中 rank 的序号,其实就表明了 over(order by salary desc) 是在where age between and 后执行的
题目答案解析:
#第一步#判断连续的核心是row_number,因为row_number是连续的#所以day-row_number,如果值是恒定的,说明也是连续的,反之一定会变化#简单的说row_number()从1开始,为每一条分组记录返回一个数字,#这里的row_number() over(partition by uid uid 是先把uid列升序,#再为升序以后的每条uid记录返回一个序号。
select uid,(day(loadtime) - row_number() over(partition by uid order by uid ) ) AS cnt from loadrecord;
#第二步#第一步得到的结果还不是很明显,需要分组用count()计数,取得不同连续值的次数SELECT uid,count(*) FROM(SELECT uid,(DAY(loadtime)-row_number() over(PARTITION BY uid ORDER BY uid) ) AS cnt FROM loadrecord) AS a GROUP BY uid,cnt;
#第三步#取得最大连续值#采用类似部门最高薪的方法 select max() group by SELECT uid, MAX(cnt) FROM(SELECT uid,count(*) AS cnt FROM(SELECT uid,(DAY(loadtime)-row_number() over(PARTITION BY uid ORDER BY uid) ) AS cnt FROM loadrecord) AS aGROUP BY uid,cnt) AS b GROUP BY uid;SELECT * from loadrecord WHERE row_number() over(partition by uid order by uid) ) AS cnt ;
About Me:小婷儿
● 本文作者:小婷儿,专注于python、数据分析、数据挖掘、机器学习相关技术,也注重技术的运用
● 作者博客地址:https://blog.csdn.net/u010986753
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● 微信:tinghai87605025 联系我加微信群
● QQ:87605025
● QQ交流群py_data :483766429
● 公众号:python宝 或 DB宝
● 提供OCP、OCM和高可用最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
如果你觉得到文章对您有帮助,点击下方我的头像,赞赏我哦!
有您的支持,小婷儿一定会越来越好!