Find the latest record SQL example

select * from #Issuer_rating

ratg_dt   ratg_clss_num

1994-04-22 00:00:00.000 1999
1994-04-28 00:00:00.000 260
1995-07-27 00:00:00.000 270
2001-03-08 00:00:00.000 270
2002-05-15 00:00:00.000 1999
——————————————–

select
 ratg_dt
 ,ratg_clss_num
 ,next_clss_num = (
  select
   distinct ratg_clss_num
  from #Issuer_rating r
  where ratg_dt>a.ratg_dt
   and not exists(
    select
     distinct 1
    from #Issuer_rating
    where  ratg_dt>a.ratg_dt
     and ratg_dt<r.ratg_dt

   )
 )

from #Issuer_rating a   order by ratg_dt asc

1994-04-22 00:00:00.000 1999 260
1994-04-28 00:00:00.000 260  270
1995-07-27 00:00:00.000 270  270
2001-03-08 00:00:00.000 270  1999
2002-05-15 00:00:00.000 1999 NULL

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>