SQL: Using Top 1 in UNION query with Order By

后端 未结 4 1263
遇见更好的自我
遇见更好的自我 2021-01-03 22:59

I have a table as below

Rate Effective_Date
---- --------------
5.6  02/02/2009
5.8  05/01/2009
5.4  06/01/2009
5.8  12/01/2009
6.0  03/15/2009
4条回答
  •  长发绾君心
    2021-01-03 23:35

    It works this way:

    select *
    from (
        select top 1 *
        from table
        where effective_date <= '05/05/2009'
        order by effective_date desc
    ) as current_rate
    
    union all
    
    select *
    from table
    where effective_date > '05/05/2009'
    

提交回复
热议问题