1

I have a table that look like this:

enter image description here

How do I write a case statement to pull out only the latest rate, which in this case would be Rate14? Anytime the rate is 0.0000, then I would need to fall back to the previous rate.

Any help would be appreciated.

4
  • 3
    Normalizing the database schema would be ideal.
    – HardCode
    Jul 20, 2016 at 17:51
  • Right. I'm an SSRS report writer and can only use what is provided. Trying to make it work. The SQL developer can not normalize the data. -sigh- Jul 20, 2016 at 17:56
  • 1
    can you explain how that work? define latest.... every comlumn after the latest is 0? in this case after Rate14 would be 0? Jul 20, 2016 at 17:57
  • 1
    You can use unpivot to change the columns into rows. That way it could be a lot easier to handle the data.
    – James Z
    Jul 20, 2016 at 18:00

3 Answers 3

7
Select
    Case When Rate15 > 0 Then Rate15
         When Rate14 > 0 Then Rate14
         ...
         When Rate2 > 0 Then Rate2
         Else Rate1
    End Rate
From Table
5
  • Simplification via brute force. Sometimes this is the best answer. Jul 20, 2016 at 18:21
  • Do i always need to go from the last rate field (rate15) to the earliest (rate1) for this case statement to work? It appears so... Jul 20, 2016 at 18:50
  • 1
    @BIGuy Definitely, because a CASE expression exits after the first critieria that hits.
    – Hart CO
    Jul 20, 2016 at 20:52
  • Aye I just wanted to answer directly as I have seen many folks who ask questions here do not have the ability to change design, at least not before they must get the current solution solved. I do agree with others that the best scenario would be to have a better design, but I did not restate what others had already commented.
    – Joe C
    Jul 20, 2016 at 21:12
  • @BIGuy the case statement should be ordered by priority, as Hart CO commented the case statement exits at the first satisfied condition.
    – Joe C
    Jul 20, 2016 at 21:14
3

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?

3

A marginally more concise approach is below but the fully expanded CASE version is probably clearer and more efficient.

Select COALESCE(NULLIF(Rate15,0),
                NULLIF(Rate14,0),
                NULLIF(Rate13,0),
                ...
                 NULLIF(Rate3,0),
                 NULLIF(Rate2,0),
                           Rate1
               ) AS Rate
From Table
1
  • I was thinking of this as well, it would be especially handy if the base data had a null instead of 0
    – Joe C
    Jul 20, 2016 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.