我有一张数据如下的表
user_id name Size lgdate
1 alpha 1.7 11-03-2019 14:00
1 alpha 0.9 11-03-2019 18:00
1 alpha 1 11-03-2019 22:00
1 alpha 3 12-03-2019 14:00
1 alpha 3.5 12-03-2019 18:00
1 alpha 3.8 12-03-2019 22:00
1 alpha 6 13-03-2019 14:00
1 alpha 6.4 13-03-2019 18:00
1 alpha 7 13-03-2019 22:00
2 beta 4 11-03-2019 14:00
2 beta 5 11-03-2019 18:00
2 beta 5.5 11-03-2019 22:00
2 beta 6 12-03-2019 14:00
2 beta 6.2 12-03-2019 18:00
2 beta 6.4 12-03-2019 22:00
2 beta 6.6 13-03-2019 14:00
2 beta 6.8 13-03-2019 18:00
2 beta 6.9 13-03-2019 22:00
我有一个查询,可以为我提供从当前日期起直到最近30天的每个日期和每个用户的最后记录的结果
select t1.*
from USER_SIZE_VIEW t1,
(select usr_id,date(lgdate), max(lgdate) as max_date
from USER_SIZE_VIEW
group by usr_id,date(lgdate) ) t2
where t1.lgdate = t2.max_date
and t1.lgdate >= DATE(NOW()) - INTERVAL 30 DAY
每个用户每天只有一条记录(最后一个条目)
user_id name Size lgdate
1 alpha 1 11-03-2019 22:00
1 alpha 3 12-03-2019 22:00
1 alpha 6 13-03-2019 22:00
1 alpha 7 14-03-2019 22:00
1 alpha 9 15-03-2019 22:00
1 alpha 10.2 16-03-2019 22:00
1 alpha 13 17-03-2019 22:00
1 alpha 17 18-03-2019 22:00
1 alpha 22 19-03-2019 22:00
1 alpha 23 20-03-2019 22:00
1 alpha 25 21-03-2019 22:00
2 beta 4 11-03-2019 22:00
2 beta 6 12-03-2019 22:00
2 beta 6.6 13-03-2019 22:00
2 beta 7.8 14-03-2019 22:00
2 beta 8 15-03-2019 22:00
2 beta 9 16-03-2019 22:00
2 beta 9 17-03-2019 22:00
2 beta 10 18-03-2019 22:00
2 beta 10 19-03-2019 22:00
我想计算尺寸列的百分比增长并将结果添加到新列以显示百分比增长
user_id name Size timestamp Size growth(%)
1 alpha 1 11-03-2019 22:00 0
1 alpha 3 12-03-2019 22:00 200
1 alpha 6 13-03-2019 22:00 100
1 alpha 7 14-03-2019 22:00 16
1 alpha 9 15-03-2019 22:00 28
1 alpha 10.2 16-03-2019 22:00 13
1 alpha 13 17-03-2019 22:00 27
1 alpha 17 18-03-2019 22:00 30
1 alpha 22 19-03-2019 22:00 29
1 alpha 23 20-03-2019 22:00 4
1 alpha 25 21-03-2019 22:00 8.6
2 beta 4 11-03-2019 22:00 0
2 beta 6 12-03-2019 22:00 50
2 beta 6.6 13-03-2019 22:00 10
2 beta 7.8 14-03-2019 22:00 18.18
2 beta 8 15-03-2019 22:00 2.56
2 beta 9 16-03-2019 22:00 12.5
2 beta 9 17-03-2019 22:00 0
2 beta 10 18-03-2019 22:00 11
2 beta 10 19-03-2019 22:00 0
编辑- 我想重新使用以上查询,并在计算中添加新列。
-谢谢