mysql - Calculating duration percentage -


i have table logs http status code of website whenever status changes, table looks this...

id    status                   date ----------------------------------- 1     404       2015-10-01 13:30:00 2     200       2015-10-02 13:30:00 3     404       2015-10-03 13:30:00 

i want use data display table on website showing how many times each status has been logged , percentage duration of status present time.

i have managed total count each status using following query....

select     `status`,     count(*) `status_count`  `table_name`  group `status`  order `status` 

...when executed gives me this...

status    status_count ---------------------- 200       1 404       2 

i modify sql add duration results calculated date column, goal end this...

status    status_count    duration (%) ----------------------------------- 200       1              20 404       2              80 

here sql fiddle demo

select t1.status       ,count(t1.id) status_count       ,sum(if(t2.date null, now(), t2.date)-t1.date) / (now()-t3.start_date) duration   table_name t1        left join table_name t2 on t1.id = (t2.id - 1)       ,(select min(date) start_date table_name) t3 group t1.status 

Comments