Question for the mysql gurus out there:
I have a data set that lists labor rates and the date on which the rate begins. These rates may be hourly, daily, weekly, or even monthly. (i.e someone worked 1 weeks at a weekly rate of $700/week starting on Nov 6th)
I’m trying to build a graph that charts daily spending over time. I have a simple sum and group query. However the weekly and monthly rates are skewing it. In the example above, all $700 lands on Nov 6th, when truly only one day’s worth ($100) of the $700 was spent.
The data set is very large, so I’m trying to let mysql do as much of the heavy lifting as possible, rather than having to loop through each row. However, if there is a conservative way to solve this programmatically, rather than through a monster query, I’m open to it.
Pretty hairy, right? Any ideas?
If this can’t be answered, my backup plan is just to store all data in terms of its daily rate, but the original rate type (week, month) will be stored as well, so that rate can be returned to.
I had to deal with this exact same issue, except it was based on hours work falling across the midnight mark.
Anyhow, business logic is what is important, it seems you want to calculate 1 week at X dollars as $(1/7)X per day.
What I would do is split this query up, first do the hourly/daily – easy, its all one day. Then do the weekly/monthly and first get the middle data which is easy. Then get the beginning-6, beginning-5, . . beginning-1 (for weekly), to start calculating the days that are in the range that you want.
If you want, you can union all these queries to get one return set.
But probably best to do it outside of mysql, you will have finer grain control. Then you can just return all your data and loop through it all real fast to process the head and tail ends.