Q:

update stats from nested query based on isnull and aggregate values

I have a parent system table called lt_program_data it contains customer data, percents tracked for that customer and a year data field, as those percents are tracked on a yearly basis. 
The percents are populated from a localized table based on some criteria, then the parent table lt_program_data is updated based on the year and customer values. 
However, in some cases we only have past data and what the user is requesting is in the cases where we have customer data, but no percents corresponding to this season we use the max season value. 
our logic is like this for now: 
update  lt_program_data
set     percent = ( select percent 
                            from #percent b 
                            where b.year = a.fyear and b.customer = a.customer)
from    lt_program_data

This works great, but now we have to say something like 
if b.year is null select Max year for that customer for the data we have. 
select * 
From #lt_program_data a 
join #percent b on b.fyear= isnull(a.fyear,max(a.fyear)) and b.customer = a.customer

I tried to write a select and then an update but get the following message:
Msg 1015, Level 15, State 1, Line 3
An aggregate cannot appear in an ON clause unless it is in a subquery contained in a HAVING clause or select list, and the column being aggregated is an outer reference.

Please help sort this out. 

Here is a sample of our output
lt_program_data 
customer year .. percent .. Other columns 
1        2016       ..
2        2016
1        2017
2        2017
3        2017

etc. 

percent table looks like this
customer year percent
1        2016  40
2        2016  64
3        2016  11

The expected result will take lt_program_data for 
customer year percent 
1        2016 40
1        2017 40
2        2016 64
2        2017 64
3        2017 NULL

It matches customer number and percent for the given year that exists in the percent table (so the value for customer 1 becomes 40 and customer 2 becomes 64) since no data for those customers exist for 2017 season, it uses the same data (max existing) data for the respective customers from 2016 season. in the case of customer 3 since there is nothing its left NULL. 
The percent table goes back to 2016, so what we want to say is since the max data we have for our customers goes back to 2016, we will populate the 2017 value in lt_program_data for customer 1 with the 2016 value of 40. 

A:

I hope this query will work for you. 
update  lt_program_data
set     percent_poverty = case 
                            when b.year is null -- year in #percent is null (no join found) 
                                then (select top 1 poverty_percent -- then get first percent by ordering year descending
                                        from #percent 
                                        where customer = a.customer 
                                        order by year desc) 
                                else b.poverty_percent -- else get the percent
                            end
from    lt_program_data a -- lets left join both tables on year and customer
left join #percent b on b.year = a.fyear and b.customer = a.customer

