mysql 按时间排序 取每5条数据的平均值、评价数

2 篇文章 0 订阅

建表语句:

CREATE TABLE `trade_params1` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `PERIOD` int(11) DEFAULT NULL,
  `THEDATE` date DEFAULT NULL,
  `DAYCOUNT` int(11) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (1,20,'2018-01-02',7);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (2,20,'2018-01-03',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (3,20,'2018-01-04',5);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (4,20,'2018-01-05',7);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (5,20,'2018-01-08',7);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (6,20,'2018-01-09',7);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (7,20,'2018-01-10',7);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (8,20,'2018-01-11',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (9,20,'2018-01-12',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (10,20,'2018-01-15',5);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (11,20,'2018-01-16',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (12,20,'2018-01-17',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (13,20,'2018-01-18',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (14,20,'2018-01-19',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (15,20,'2018-01-22',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (16,20,'2018-01-23',6);
INSERT INTO `trade_params1` (`ID`,`PERIOD`,`THEDATE`,`DAYCOUNT`) VALUES (17,20,'2018-01-24',6);

 

执行sql 先按时间排序 然后查询出每行的行号 按行号分组

SELECT avg(tt.daycount) FROM (
SELECT t.daycount,(@rownum := @rownum + 1) - 1 AS rownum FROM (SELECT @rownum := 0) r,trade_params1 t 
    WHERE t.THEDATE < '2019-01-18' ORDER BY  THEDATE desc
) tt GROUP BY tt.rownum - tt.rownum % 5

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值