I have a Account Balance History table containing ACCT_ID
, EFFDATE
, and AMT
. ACCT_ID
and EFFDATE
are PK. A new AMT
is only recorded when it changes, which doesn't happen every day.
What I want to do is create a query that will give me daily totals for each of the last 5 days where EFFDATE
is less <= one of the 5 calendar dates. Then problem I'm having is that not I don't have a Date table to queue off of to find AMT
.
I am unable to alter the database in any way.
Here is sample data:
+---------+------------+------+
| ACCT_ID | EFFDATE | AMT |
+---------+------------+------+
| 1112 | 12/30/2018 | 500 |
| 1112 | 12/25/2018 | 675 |
| 1112 | 12/21/2018 | 187 |
| 1112 | 12/19/2018 | 332 |
| 1112 | 12/10/2018 | 92 |
| 1112 | 12/3/2018 | 300 |
| 2223 | 12/29/2018 | 225 |
| 2223 | 12/20/2018 | 100 |
| 2223 | 12/11/2018 | 525 |
| 3334 | 12/31/2018 | 1000 |
| 3334 | 12/20/2018 | 600 |
| 3334 | 12/14/2018 | 490 |
| 3334 | 12/1/2018 | 350 |
| 4445 | 12/20/2018 | 300 |
| 4445 | 12/19/2018 | 250 |
| 4445 | 12/12/2018 | 200 |
| 4445 | 12/9/2018 | 150 |
| 4445 | 12/6/2018 | 100 |
| 4445 | 12/1/2018 | 50 |
+---------+------------+------+
I can successfully sum AMT
for the most recent day with this script:
SELECT sum(amt) from (
select
ACCT_ID, EFFDATE, AMT, ROW_NUMBER() over (partition by acct_id order by effdate desc) Rn
from BALHIST
) q where q.Rn = 1
Assuming today is 1/1/19, how can I get total balance for Today and the last 5 days?
EDIT: Expected Results
+------------+----------+
| Date | Sum(AMT) |
+------------+----------+
| 12/31/2018 | 2025 |
| 12/30/2018 | 1625 |
| 12/29/2018 | 1800 |
| 12/28/2018 | 1625 |
| 12/27/2018 | 1625 |
| 12/26/2018 | 1625 |
| 12/25/2018 | 1625 |
| | |
| 12/20/2018 | 1282 |
| 12/19/2018 | 1597 |
| 12/18/2018 | 1307 |
+------------+----------+
Manual calculation sample:
12/31/2018: ([1112,12/30/2018] + [2223,12/29/2018] + [3334,12/31/2018] + [4445,12/20/2018]) = 2025
12/26/2018: ([1112,12/21/2018] + [2223,12/20/2018] + [3334,12/20/2018] + [4445,12/20/2018]) = 1675
Best Answer
This gives you the expected output for a given date range. May not be the most elegant though, but it includes an example of creating a Date table on the fly, although for large date ranges it will perform poorly.