Grouping MySQL results by 7 day increments -
hoping might able assist me this.
assume have table listed below. hosts can show multiple times on same date, different backupsizes.
+------------------+--------------+ | field | type | +------------------+--------------+ | startdate | date | | host | varchar(255) | | backupsize | float(6,2) | +------------------+--------------+
how find sum total of backupsize 7 day increments starting earliest date, through last date? don't mind if last few days cut off because don't fall 7 day increment.
desired output (prefered):
+------------+----------+----------+----------+----- |week of | system01 | system02 | system03 | ... +------------+----------+----------+----------+----- | 2014/07/30 | 2343.23 | 232.34 | 989.34 | +------------+----------+----------+----------+----- | 2014/08/06 | 2334.7 | 874.13 | 234.90 | +------------+----------+----------+----------+----- | ... | ... | ... | ... |
or
+------------+------------+------------+------ |host | 2014/07/30 | 2014/08/06 | ... +------------+------------+------------+------ | system01 | 2343.23 | 2334.7 | ... +------------+------------+------------+------- | system02 | 232.34 | 874.13 | ... +------------+------------+------------+------- | system03 | 989.34 | 234.90 | ... +------------+------------+------------+------- | ... | ... | ... |
date format not concern, long gets identified somehow. also, order of hosts not concern either. thanks!
the simplest way earliest date , count number of days:
select x.minsd + interval floor(datediff(x.minsd, lb.startdate) / 7) day `week of`, host, sum(backupsize) listedbelow lb cross join (select min(startdate) minsd listedbelow lb) x group floor(datediff(x.minsd, lb.startdate) / 7) order 1;
this produces form week of
, host
on each row. can pivot results see fit.
Comments
Post a Comment