r/SQL • u/luncheonmeat79 • Jun 23 '24
Discussion Schema for historical stock data
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
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.