简体   繁体   中英

How to store statistics in database

I have the following table

Restaurant Type, Location, HowMany
-----------------------------------
Chinese, New York, 20
Chinese, London, 40
Indian, New York, 33
Indian, Sydney, 55

The numbers in the How Many column change over time, and new rows could also be added to the table.

What I am trying to achieve is some statistics (eg monthly or weekly), to show the how the 'HowMany' Column values has changed over time for a particular combination of restaurant type/location.

i was thinking of taking a snapshot of the table every month/week and gathering the statistics that way, but I was hoping there might be a better solution.. any solution that would make it easy for me to display the data in a bar graph.

Is there a way better than storing a blob of data?

I would suggest creating an historical table with a timestamp that links to the main table with a foreign key. Create a function that inserts into the historical table with a timestamp and the old value every time the main table is changed.

ID, RestaurantType, Location, HowMany
---------------------------------------------------------------
1, Chinese, New York, 20

The historical will look like

ID, MainTableID, Value, DateChanged
----------------------------------------------------------
1, 1, 18, 8/1/2015
2, 1, 19, 8/5/2015

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