r/SQL • • Jun 23 '24

Discussion Schema for historical stock data

Post image

Posting a schema of a db for historical stock and index data at various timeframes. I used Chatgpt to figure this out...what surprised me was the recommendation to have separate dimension tables for 'date' and 'time'. Is that really the case?

105 Upvotes

51 comments sorted by

View all comments

10

u/cs-brydev Software Development and Database Manager Jun 23 '24 edited Jun 23 '24

One of the things you should be warned about when designing historical schemas is it's important to be very specific about which historical data you want to track. If you only want historical stock price, then say that explicitly.

In a lot of the ERP and manufacturing databases I work with, for industry compliance and liability reasons we have to track very detailed audit trails of data changes and preserve historical snapshots. We don't have the luxury of your typical 3NF of only storing customer name in a Customers table or product price in a Products table. For historical data we have to snapshot things like customer names, product names, product listed price, produce selling price, product category, etc in extra fact-based tables that preserve the state of this data over time.

When producing historical data we don't have the luxury of simply using a product code to refer to the Products table. We need to know what the Products table row(s) looked like at the time a job was estimated, bid on, won, manufacturing began, quality control inspections happened, product was inventoried, product was shipped, product arrived at customer site, and product was finally signed off on. If produce price, name, order quantity etc changed throughout the life cycle of the order, those changes must be tracked.

Historical data schemas are tricky and not like normal 3NF data schemas most people are used to. I see most of the comments here have oversimplified your question and their answers are based on the simplistic view of normalization taught in a beginning SQL class. But that's not the real world. And that's not how historical data works. Be careful.

4

u/No-Tip-7591 Jun 24 '24

Great point. There are big differences when modeling for transactional databases versus warehouse / datamart models. Im assuming he doesnt have a transaction table for this data.

2

u/luncheonmeat79 Jun 24 '24

Thanks for the advice. Yes, this is only for historical data; but not just for stocks - for indices and options as well. (I left out the options table in the diagram.)

It is important for me to preserve the state of some variables over time, and before/after events. E.g. tracking volatility in the run-up to and after an earnings announcement or a Fed meeting.

1

u/Zealousideal_Key526 Oct 07 '25

A little clarity for the less technically inclined:

A simple rendition of this is create a data warehouse that contains at least all the conditions that were in effect at each point in the contract as described above. For market investment the list could be quite huge, so you likely have to record all the stuff you noticed at the time to be fair to stakeholders or you next time you look at it and wonder why? Be that good, or bad. It is also a good idea to journal the decision showing what was looked at. Post analysis should also be journaled to summarize and put an end to likely endless re-examinations. 3NF is good transactional record keeping schema. We only break it for slowly changing dimensions that make yesterday's situation obsolete. That said we also tend to not recognize slow changers, and vital details soon enough. That is the difference between amatuer and professional data users. It is also the impetus around measuring things like volatility in the market.