mysql - Grouping by X days -


i have database shows me different stats different campaigns, each row has timestamp value name "date".

i wrote code choosing , summarizing range of dates, example: 21-24/07/2010. need add option choose range of dates, group stats each x days.

let's user chooses see stats month: 01/07-31/07. present him stats grouped x days, let's 3, see stats 01-03/07, 04-06/07,07-09/07 , on...

i managed doing using code:

select t1.camp_id,from_days( floor( to_days( date ) /3 ) *3 ) 'first_date' facebook_stats t1 inner join facebook_to_campaigns t2 on t1.camp_id = t2.facebook_camp_id date between 20100717000000 , 20100724235959 group from_days( floor( to_days( date ) /3 ) *3 ) , t2.camp_id 

it group (by 3 days), problem reason starts 16/07, , not 17/07, grouping each time 3 days @ time.

would love hear solution code or gave, or better solution have in mind.

to_days returns number of days since year 0. when divide 3, considers quotient , not remainder. eg. if has been 5 days since year 0, to_days return 1.

to_days(20100717000000) must leaving remainder of 1. to_days(20100716000000) divisible 3 17th not.

you try query:

declare @startdate datetime declare @enddate datetime declare @groupbyinterval int set @startdate = 20100717000000 set @enddate = 20100724235959 set @groupbyinterval = 3  select  t1.camp_id,   from_days( to_days(@startdate)+floor( (to_days(date)-to_days(@startdate))/@groupbyinterval ) * @groupbyinterval)  first_date facebook_stats t1 inner join facebook_to_campaigns t2 on t1.camp_id = t2.facebook_camp_id date between @startdate , @enddate group first_date , t2.camp_id 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -