Grafana mysql time series - using multiple queries in loop within timefilter?

Hey
I have a table with such structure
id (primary), mac, value, timestamp
with constraint unique(mac, timestamp)

what I’m trying to do

  1. divide all timerange in timestamps to timegroups like 30min
  2. for each group get summary of value and display it on graph
    2.1 if some mac appears few times within timeslot - we should choice only one value

when I tried to achieve goal above - I got successful request for specified timerange, but I don’t understand how to modify it for grafana
here is my sql request
select count(*), sum(s.value) from (select hist_data.mac, hist_data.value from hist_data where timestamp between ‘2018-10-07 17:00:00’ and ‘2018-10-07 17:29:59’ group by hist_data.mac) s;

I would like to have a bunch of such calculated points on timeseries graph within __$timeFilter divided by every hour for example

how to do that ?
I feel like I should divide $timeFilter by hour, than iterate over all values and finally get this on graph
but there is no ability to use procedure and variables etc… I’m stuck