clear markdown compare help donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 497219 fiddles created (9447 in the last week).

CREATE TABLE `stats_kpi` ( `group_id` bigint(20) NOT NULL, `value` double unsigned DEFAULT NULL, `ts_week_start` timestamp NULL DEFAULT CURRENT_TIMESTAMP );
 hidden batch(es)


INSERT INTO `stats_kpi` VALUES (1, 1, timestamp('2018-01-01')) ,(1, 2, timestamp('2018-01-02')) ,(1, 3, timestamp('2018-01-03')) ,(1, 4, timestamp('2018-01-04')) ,(1, 5, timestamp('2018-01-05')) ,(2, 1, timestamp('2018-01-01')) ,(2, 2, timestamp('2018-01-02')) ,(2, 3, timestamp('2018-01-03')) ,(2, 4, timestamp('2018-01-04')) ,(2, 5, timestamp('2018-01-05'));
 hidden batch(es)


SET @date:='2018-01-03';
 hidden batch(es)


select * from ( select ta.*, if(@typex=ta.group_id, @rownum:=@rownum+1, @rownum:=1+least(0,@typex:=ta.group_id)) rown from stats_kpi ta, (select @rownum:=1, @typex:='_') zz where ts_week_start <= timestamp(@date) order by group_id, ts_week_start DESC ) yy where rown < 3
group_id value ts_week_start rown
1 3 2018-01-03 00:00:00 1
1 2 2018-01-02 00:00:00 2
2 3 2018-01-03 00:00:00 1
2 2 2018-01-02 00:00:00 2
 hidden batch(es)