SQL SERVER中FIRST_VALUE和LAST_VALUE

來源:互聯網
上載者:User

SQL SERVER中FIRST_VALUE和LAST_VALUE


FIRST_VALUE和LAST_VALUE

看下組SQL語句:

WITH testas(    select '樂可樂可的部落格' as name,10 as score    UNION ALL    select '樂可樂可的部落格',15    UNION ALL    select '樂可樂可的部落格',20    UNION ALL    select '微軟認證',30    UNION ALL    select '微軟認證',40    UNION ALL    select '微軟認證',40)select name,score,FIRST_VALUE(score)over(order by name) as fst,LAST_VALUE(score)over(order by name) as Lstfrom test


結果:

name                         score    fst    Lst
樂可樂可的部落格     15         15    10
樂可樂可的部落格     20         15    10
樂可樂可的部落格     10         15    10
微軟認證                    40         15    30
微軟認證                    40         15    30
微軟認證                    30         15    30

FIRST_VALUE(score)over(order by name) as fst,取按name升序排列的第一行score,見紅色字型。

LAST_VALUE(score)over(order by name) as Lst,取按name升序排列的相同name最後一行score,見藍色和紫色字型。




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.