sql - Selecting two fields recursively for each month -


indroduction

i'm using freeradius , i'd query table, radacct , select 2 fields (datetime fields) within given year , provide sumof bytes_send , bytes_received per month.

my database layout

fieldtype    fieldname ---------    ---------- varchar      username datetime     start datetime     end bigint       bytes_send bigint       bytes_received 

database data

username    start                  end                      bytes_send    bytes_received ---------   -------------------    -------------------      ----------    -------------- admin       2013-04-22 22:14:34    2013-04-22 22:24:51      8723142       851314 admin       2013-05-14 11:50:50    2013-05-15 11:58:35      11073753      958437 admin       2013-05-14 14:36:24    2013-05-15 15:11:59      869125711     3152551 admin       2013-05-15 20:45:16    2013-05-15 20:47:11      2373753       114857 demo        2013-05-14 23:21:51    2013-05-15 05:32:20      59125919      3815816 

desired output

i'd have query puts out (the sql query needs know year (2013 in case) , username (admin in case).

username   month  total_bytes --------   -----  ----------- admin      04     9574456 admin      05     886799062 

my solution

i'd php, set year (2013 in case) , perform 12 queries (one each month) using simple loop.

my question

the solution proposed not best solution, hence i'd know if done single query?

a simple aggregate it

select     username, month(start), sum(bytes_send+bytes_received)     mytable      -- avoiding year function on predicate     start >= '20130101' , start < '20140101'     ,     username = 'admin' group     username, month(start) 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -