aggregation - MySQL - how do i group compare data with a previous row? -
i have table following data:
mysql> select * playground order macaddress, locatedat; +------------+---------------------+--------------+ | macaddress | locatedat | status | +------------+---------------------+--------------+ | device1 | 2014-08-11 01:20:27 | connected | | device1 | 2014-08-11 01:30:27 | connected | | device1 | 2014-08-11 01:40:27 | disconnected | | device1 | 2014-08-11 01:49:27 | connected | | device1 | 2014-08-11 01:50:27 | disconnected | | device1 | 2014-08-11 01:55:27 | disconnected | | device1 | 2014-08-11 02:05:27 | disconnected | | device1 | 2014-08-11 02:17:27 | disconnected | | device1 | 2014-08-11 02:27:27 | disconnected | | device1 | 2014-08-11 02:47:27 | connected | | device1 | 2014-08-11 02:57:27 | connected | | device1 | 2014-08-11 03:50:27 | disconnected |
i need summarized below:
date | hour | connected | disconnected | previouslyconnected 2014-08-11 | 1 | 1 | 0 | 0 2014-08-11 | 2 | 1 | 0 | 0 2014-08-11 | 3 | 0 | 0 | 1
as long device connected once in hour, disconnected doesn't matter. if device has 'disconnected' entries given hour, it's in 'disconnected' state. , if device disconnected, gets previouslyconnected state if same device in 'connected' state in previous hour. otherwise stays in 'disconnected' state itself.
in third row, device1 in 'previouslyconnected" state because device connected in previous hour (hour 2).
i have trouble getting 'previouslyconnected' state devices;
with current query, able except last column.
select temp.day, temp.hour, sum(case when temp.connected > 0 1 else 0 end) connected, sum(case when temp.unconnected > 0 , temp.connected < 1 1 else 0 end) unconnected (select date(locatedat) day, hour(locatedat) hour, macaddress, sum(case when status='connected' 1 else 0 end) connected, sum(case when status='disconnected' 1 else 0 end) unconnected playground group date(locatedat), hour(locatedat), macaddress) temp group temp.day, temp.hour;
any pointers?
you can use user-defined variable in order check previous state of result ,in inner query have used group_concat
concatenate statuses in descending order of locatedat
generate comma separated string of statuses in descending order , using substring_index
have picked first status last state of machine, , have stored last state expression substring_index(group_concat(
statusorder locatedat desc),',',1)
in @prev
variable ,and case
statement check if connected in previous row show 1 else 0
select temp.day, temp.hour, sum(temp.connected > 0) connected, sum(temp.unconnected > 0 , temp.connected < 1) unconnected, temp.previouslyconnected ( select date(locatedat) day, hour(locatedat) hour, macaddress, sum(status='connected') connected, sum(status='disconnected') unconnected, case when @prev = 'connected' 1 else 0 end previouslyconnected, @prev:= substring_index(group_concat(`status` order locatedat desc),',',1) playground join (select @prev:='') t group date(locatedat), hour(locatedat), macaddress ) temp group temp.day, temp.hour;
Comments
Post a Comment