简体   繁体   中英

What's the best way to handle periods of time yearly in a database?

Im trying to figure out how can I handle periods of time in a year. A period can be weekly, monthly and every 14 days.

For a weekly period, assuming the year 2014, period 1 it should be from Dec 23 2013 - Jan 04 2014. So I need to have this.

Period 1: Dec 23 2013 - Jan 04 2014 Period 2: Jan 05 2014 - Jan 11 2014 Period 3: Dec 12 2014 - Jan 18 2014 ..... ..

So having that, today being June 11 2014, the period would be period 24, for every 14 days we would have:

Period 1: Dec 23 2013 - Jan 11 2014 Period 2: Jan 12 2014 - Jan 25 2014 ... ..

And so on.

Each year the periods need to start over from 1, having said that Period 1 weekly of 2015 would be from Dec 30 2014 - Jan 06 2015. Also, I should be able to have a periods page where I click in Period 1 Weekly of 2014 and get a report from that period.

I have a periods table with id, period_no, from_date, to_date, type (Weekly, monthly, every 14 days), and another table that it's related to periods table by period_id. So I want to know what would be the best way to handle this? should I initially set a certain number of periods? for instance let's assume 2015 it's tomorrow, so tomorrow the system automatically would fill the periods table with all the corresponding periods for this year? or should I create periods manually? or don't save periodos at all and have them all calculated at the moment I make a request without saving in a DB?

I hope I made myself clear.

Keep timestamps or datetimes, and GROUP BY whatever is appropriate for your usage.

MySQL: group by date RANGE?

If you need a fast report, and you have a ton of data (ie: Your SELECT is slow), you can build a reporting table that update your groups appropriately.

very big mysql table and reporting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM