20 likes | 150 Vues
This guide outlines an SQL method for retrieving the balance of an account on a specific date by identifying the most recent transaction prior to that date. It utilizes surrogate keys to ensure consistent date ordering. The SQL query selects account numbers and their corresponding balances from a fact table and an account table, ensuring that it captures the last transaction before the given date. This approach is essential for accurate financial reporting and historical data analysis in database management systems.
E N D
To Find the Balance at a Point-in-time SQL Account fact Date Account dateKey (FK) branchKey (FK) productKey (FK) accountKey(FK) accountStatusKey (FK) householdKey (FK) balance … dateKey(PK) fullDate … accountKey(PK) accountNum … To find the balance as of a certain date requires finding the last transaction prior to the date The following assumes the date surrogate keys are “increasing” as the date “increases”
To Find the Balance at a Point-in-time SQL Account fact Date Account dateKey (FK) branchKey (FK) productKey (FK) accountKey(FK) accountStatusKey (FK) householdKey (FK) balance … dateKey(PK) fullDate … accountKey(PK) accountNum … SELECT accountNum, balance FROM fact f, account a WHERE f.accountKey = a.accountKey AND f.dateKey = (select max(g.dateKey) FROM fact g WHERE g.accountKey = f.accountKey AND g.dateKey IN (SELECT d.dateKey FROM date d WHERE d.fullDate <= 'January 5, 2002') )