I would like to build a very basic daily sales report. I’m am trying to decide how to structure the database to best accomplish this. Here is a use case for it:
- On Jan 5, 2011, Provider A makes $500 total off of its products
- On Jan 5 2011, Provider A makes $200 total off of its products
- On Jan 6, 2011, Provider B makes $450 total off of its products
- On Jan 6, Provider B makes $75 total off of its products
The current structure I have is:
PROVIDER table
- pk
- provider
PRODUCT table
- provider (FK)
- product
- start_date (sales)
- end_date
The start_date and end_date are when sales on the product may occur. It is only used for reference, and does not really affect anything else.
SALES table
- product (FK)
- sales
- How to store date?
sales would be the daily proceed ($) for sales from that product.
I’m not quite sure how to store the sales. Sales would only be calculated as a daily sum for each product. What would be the best way to structure the SALES table?
Product Table:
Sales Table:
With the aforementioned setup, you can change prices as you see fit in the
producttable, but it won’t affect already occurred sales recorded in thesales_transactiontable.Pseudo schema:
product:sales_transaction:provider:Now, you can run queries to get summations of any product for any date for any provider, as you requested in your question.
Sample Query:
The schema provided is skeletal in nature, so feel free to add columns as your business requirements dictate, but it should get you going in the right direction.
Also, a final piece of advice, I would recommend storing your datetimes in UTC. If you opt to store in local timezone you will run into any number of headaches if you do any sales requiring conversion from your local timezone.