基本上我有一个时间序列数据,我们希望按记录时间进行汇总。偶尔会在同一秒内多次拉取数据,因此在聚合值加倍或三倍时。有没有什么方法可以group by
记录时间而不对给定列的一组具有相同值的行进行分组?
即
09:05:00 FRONT 100
09:05:00 FRONT 100
09:05:00 BACK 25
09:05:00 BACK 25
我想得到:
09:05:00 125
09:05:00 125
答案 0 :(得分:0)
拿#2
我添加了额外的几行以使逻辑更清晰 - 这应该产生3个相同的行:
time, label, value
'09:05:00', 'FRONT', '100'
'09:05:00', 'FRONT', '100'
'09:05:00', 'BACK', '25'
'09:05:00', 'BACK', '25'
'09:05:00', 'FRONT', '100'
'09:05:00', 'BACK', '25'
这是看起来工作的SQL(由于我的mysql安装不允许我使用'with'而创建的视图 - 通常没有一个显示最好的mysql - row_number(),任何人?):
create view myview as
select s1.time, s1.value from
(select time,sum(value) as value from mytable group by time) s1
join (select * from mytable) s2
on s1.time = s2.time
join (select time, min(label) as minLabel from mytable group by time) s3
on s2.label = s3.minLabel;
select m1.time, cast(value/numRows as signed) as value from myview m1
join (select time, count(*) as numRows from myview) m2
on m1.time=m2.time
结果:
time, value
'09:05:00', '125'
'09:05:00', '125'
'09:05:00', '125'