Totally agree with @Joe C (+1), "brute force" is the simplest way. Just because, I went through the exercise of building the unpivot for this, if for no other reason than to justify the simplicity of the big case statement. (Also, it is much easier to deal with the "no data" situation with the big case statement.)
First, set up some sample data:
--DROP TABLE MyTable
GO
CREATE TABLE MyTable
(
CUSIPNumber varchar(20) not null
,Year smallint not null
,Rate1 float not null
,Rate2 float not null
,Rate3 float not null
,Rate4 float not null
,Rate5 float not null
,Rate6 float not null
,Rate7 float not null
,Rate8 float not null
,Rate9 float not null
,Rate10 float not null
,Rate11 float not null
,Rate12 float not null
,Rate13 float not null
,Rate14 float not null
,Rate15 float not null
)
INSERT MyTable values
('001383EA2', 16, 4.0505, 4.0510, 4.0515, 4.0520, 4.0525, 4.0530, 4.0535, 4.0550, 4.0545, 4.0550, 4.0560, 4.0570, 4.0575, 4.0585, 0)
,('001383EA2', 15, 3.0505, 3.0510, 3.0515, 3.0520, 3.0525, 3.0530, 3.0535, 3.0550, 3.0545, 3.0550, 3.0560, 3.0570, 3.0575, 3.0585, 3.0599)
,('001383__1', 01, 1.1, 2.2, 3.3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
,('001383_NoData', 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
-- What we have
SELECT *
from MyTable
Here's the basic unpivot statement. Columns are aliased/renamed, to allow for max() to identify the latest item (Rate2 is > Rate11...)
-- Build the unpivot statement
SELECT *
from (select CUSIPNumber, Year, Rate1, Rate3, Rate4, Rate5, Rate6, Rate7, Rate8, Rate9, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15
from MyTable) src
unpivot (RateValue
for RateNumber in (Rate1, Rate3, Rate4, Rate5, Rate6, Rate7, Rate8, Rate9, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15)
) unpvt
-- Whoops! Alias the "Rate" columns, so that they can be sorted
SELECT *
from (select
CUSIPNumber
,Year
,Rate1 Rate01
,Rate2 Rate02
,Rate3 Rate03
,Rate4 Rate04
,Rate5 Rate05
,Rate6 Rate06
,Rate7 Rate07
,Rate8 Rate08
,Rate9 Rate09
,Rate10
,Rate11
,Rate12
,Rate13
,Rate14
,Rate15
from MyTable) src
unpivot (RateValue
for RateNumber in (Rate01, Rate02, Rate03, Rate04, Rate05, Rate06, Rate07, Rate08, Rate09, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15)
) unpvt
This data needs to be referenced multiple times. Simplest way to do that is to make it a CTE:
-- Make it a Common Table Expression
WITH ctePvt
as (select CUSIPNumber, Year, RateNumber, RateValue
from (select
CUSIPNumber
,Year
,Rate1 Rate01
,Rate2 Rate02
,Rate3 Rate03
,Rate4 Rate04
,Rate5 Rate05
,Rate6 Rate06
,Rate7 Rate07
,Rate8 Rate08
,Rate9 Rate09
,Rate10
,Rate11
,Rate12
,Rate13
,Rate14
,Rate15
from MyTable) src
unpivot (RateValue
for RateNumber in (Rate01, Rate02, Rate03, Rate04, Rate05, Rate06, Rate07, Rate08, Rate09, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15)
) unpvt)
select *
from ctePvt
With this, we can determine the most recent Rate
-- This determines the most recent rate
WITH ctePvt
as (select CUSIPNumber, Year, RateNumber, RateValue
from (select
CUSIPNumber
,Year
,Rate1 Rate01
,Rate2 Rate02
,Rate3 Rate03
,Rate4 Rate04
,Rate5 Rate05
,Rate6 Rate06
,Rate7 Rate07
,Rate8 Rate08
,Rate9 Rate09
,Rate10
,Rate11
,Rate12
,Rate13
,Rate14
,Rate15
from MyTable) src
unpivot (RateValue
for RateNumber in (Rate01, Rate02, Rate03, Rate04, Rate05, Rate06, Rate07, Rate08, Rate09, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15)
) unpvt)
select
CUSIPNumber
,Year
,max(RateNumber) CurrentRate
from ctePvt
where RateValue <> 0
group by
CUSIPNumber
,Year
And with that, we can finally get the value for the most recent rate
-- This gest the values for the most recent rate
WITH ctePvt
as (select CUSIPNumber, Year, RateNumber, RateValue
from (select
CUSIPNumber
,Year
,Rate1 Rate01
,Rate2 Rate02
,Rate3 Rate03
,Rate4 Rate04
,Rate5 Rate05
,Rate6 Rate06
,Rate7 Rate07
,Rate8 Rate08
,Rate9 Rate09
,Rate10
,Rate11
,Rate12
,Rate13
,Rate14
,Rate15
from MyTable) src
unpivot (RateValue
for RateNumber in (Rate01, Rate02, Rate03, Rate04, Rate05, Rate06, Rate07, Rate08, Rate09, Rate10, Rate11, Rate12, Rate13, Rate14, Rate15)
) unpvt)
select
cteBase.CUSIPNumber
,cteBase.Year
,cteBase.RateValue
from ctePvt cteBase
inner join (-- Most recent rate per item
select
CUSIPNumber
,Year
,max(RateNumber) CurrentRate
from ctePvt
where RateValue <> 0
group by
CUSIPNumber
,Year) cteRecent
on cteRecent.CUSIPNumber = cteBase.CUSIPNumber
and cteRecent.Year = cteBase.Year
and cteRecent.CurrentRate = cteBase.RateNumber
Note that this returns no row for the item where there are no rate values. There are a handful of ways that can be resolved, but enough.
Aren't you glad you went with the big case statement?
latest
.... every comlumn after the latest is 0? in this case afterRate14
would be 0?