可以写出从数据上可以直观看出分组的sql, 另外对于"分组的时间间隔小于30分钟"这句话可能引申出两种理解:
数据先按时间排序, 只要相邻数据DEAL_TIME在30分钟以内则归组, 比如2019-06-06 13:00:00, 2019-06-06 13:05:00和2019-06-06 13:32:00可以归为一组
数据先按时间排序, 对于理解1的3条数据, 此时2019-06-06 13:32:00和2019-06-06 13:00:00的间隔超过30分钟, 需要另起一组
对于上述两个理解我这边都给出例子, 原理和MySql的rownumber实现有些类似, 排序后加上条件滚动计算, 方案有不足的地方, 1是多一层排序嵌套子查询, 2是结果如果不再嵌套会多出计算过程列
以下为代码:
-- by sleest 2019/06/06 临近分组-- 制造临时表数据用于验证
DROP TEMPORARY TABLE IF EXISTS TMP_DEAL_TIME
CREATE TEMPORARY TABLE TMP_DEAL_TIME(DEAL_TIME DATETIME) AS
SELECT DATE_ADD(CURDATE(), INTERVAL 600 * RAND() MINUTE) AS DEAL_TIME
FROM (SELECT 1 UNION ALL SELECT 2 UNION SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5) A,
(SELECT 1 UNION ALL SELECT 2 UNION SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5) B
-- 查看临时表数据
SELECT DEAL_TIME FROM TMP_DEAL_TIME ORDER BY DEAL_TIME
-- 理解1: 只要相邻时间为指定分钟数以内则聚合
SELECT DEAL_TIME,
CASE WHEN @lastDealTime IS NULL THEN @minLevel:=1
WHEN DEAL_TIME > DATE_ADD(@lastDealTime, INTERVAL 30 MINUTE)
THEN @minLevel:=@minLevel+1 END AS COMPUT_CONDITION,
@lastDealTime:=DEAL_TIME AS LAST_DEAL_TIME,
@minLevel AS GROUP_LEVEL
FROM (SELECT DEAL_TIME FROM TMP_DEAL_TIME ORDER BY DEAL_TIME) E,
(SELECT @lastDealTime:=NULL) F,
(SELECT @minLevel:=0) G
-- 理解2: 只和开始聚合的第一条时间比较, 只有当该条数据处在聚合第一条指定分钟数以内才归组
SELECT DEAL_TIME,
CASE WHEN @lastDealTime IS NULL THEN @minLevel:=1
WHEN DEAL_TIME > DATE_ADD(@lastDealTime, INTERVAL 30 MINUTE)
THEN @minLevel:=@minLevel+1 END AS COMPUT_CONDITION,
CASE WHEN @lastDealTime IS NULL THEN @lastDealTime:=DEAL_TIME
WHEN DEAL_TIME > DATE_ADD(@lastDealTime, INTERVAL 30 MINUTE)
THEN @lastDealTime:=DEAL_TIME END AS LAST_DEAL_TIME,
@minLevel AS GROUP_LEVEL
FROM (SELECT DEAL_TIME FROM TMP_DEAL_TIME ORDER BY DEAL_TIME) E,
(SELECT @lastDealTime:=NULL) F,
(SELECT @minLevel:=0) G
对于理解1的执行结果如下:
对于理解2的执行结果如下:
后续可以根据结果中的GROUP_LEVEL进行分组显示
1、单列排序SELECT * FROM test1 ORDER BY date_time
默认升序,降序后面接"DESC"即可。
2、多列排序
SELECT * FROM test1 ORDER BY `status`, date_time DESC
首先按`status`字段排序,若`status`相等,则按data_time排序。
3、自定义排序
SELECT * FROM test1 ORDER BY FIELD(`status`, 3, 2, 4, 1, 5), date_time DESC
使用"FIELD()"函数,可指定顺序。
4、其他条件排序
先按大于等于当前时间升序,再按小于当前时间降序,支持分页。
SELECT * FROM test1 ORDER BY date_time <NOW(), IF(date_time <NOW(), 0, date_time), date_time DESC
附加SQL脚本:
CREATE TABLE `test1` (`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`date_time` datetime NOT NULL,
`status` int(5) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
INSERT INTO `test1` VALUES
(NULL, '测试1', '2018-03-05 11:09:00', 1),(NULL, '测试2', '2018-03-06 11:09:00', 1),(NULL, 'abc', '2018-03-07 11:09:00', 1),
(NULL, 'def', '2018-04-08 11:09:00', 2),(NULL, '李某某', '2018-04-17 11:09:00', 1),(NULL, '饭某某', '2018-04-20 13:09:00', 2),
(NULL, '赵', '2018-04-20 01:09:00', 4),(NULL, '倩', '2018-04-28 11:09:00', 2),(NULL, 'andy', '2018-04-30 11:09:00', 1),
(NULL, 'tony', '2018-05-08 11:09:00', 4),(NULL, 'tom', '2018-05-07 11:09:00', 3),(NULL, 'bill', '2018-05-18 11:09:00', 3),
(NULL, 'james', '2018-06-07 11:09:00', 4),(NULL, 'anthony', '2018-06-18 11:09:00', 2),(NULL, '盖茨', '2018-04-21 11:09:00', 1),
(NULL, '部长', '2018-04-24 11:09:00', 4),(NULL, '李总', '2018-04-20 11:09:00', 5),(NULL, '张总', '2018-04-29 11:09:00', 2),
(NULL, '王总', '2018-04-19 11:09:00', 3),(NULL, '唐总', '2018-05-01 11:09:00', 2)
参考的这篇文档Mysql排序方式
用union拼接一下就行了,形如selet * from tablename where hdtime>='2013-5-6' order by hdtime
union all
selet * from tablename where hdtime<'2013-5-6' order by hdtime desc
当然这不是标准的语法,mysql中的时间比较偶不太清楚,你要自己修改一下。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)