So I’m trying to find the total budget of each account in the most current week. The sample data would look have 3 columns Account/Week/Budget. The desired outcome would be the "Current Budget" Column. So that when I sum the "Current Budget" column I would sum the most recent data of each account for what the projected budget is. If there is no data on the account for the current week (3), it would look at the previous week’s data (2) instead.
Account | Week | Budget | Current budget |
---|---|---|---|
1 | 1 | $24 | null |
2 | 1 | $100 | null |
3 | 1 | $30 | null |
1 | 2 | $100 | null |
2 | 2 | $24 | null |
3 | 2 | $100 | $100 |
1 | 3 | $100 | $100 |
2 | 3 | $24 | $24 |
So far this is what I have. But I run into the issue where I would have last weeks budget for accounts that already have a week 3 budget.
declare @currentweek = datepart(week, getdate())
declare @lastweek = @currentweek -1
select t.*,
case
when t.week = @currentweek then t.budget
when t.week = @lastweek then t.budget
else null
from
table t
2
Answers
First thing that comes to my mind is using a subquery to know which is the most recent week of each account, like the following code:
You could also use an auxiliary table:
Please, let me know if I didn’t understand your necessity correctly
I would consider something like
In the common table expression
RANK()
is used to figure out the most recent week (so the main query can have a special case forwr = 1
which is the most recent week). I usedCASE WHEN wr = 1 THEN SUM(budget) ELSE NULL END
even though I thinkSUM(CASE WHEN wr = 1 THEN budget END)
is easier to read, since the latter will give a warning about nulls being eliminated by the aggregate.You can see it working in this Fiddle.