SQL Server->> First_value and Last_value functions

Source: Internet
Author: User

Two are all functions introduced by SQL SERVER 2012. Used to return the value of a field that gets the last row after grouping and sorting. It's a simple two function. The ORDER by clause is required, and parition by is optional.

There seems to be nothing to say. The result is a problem when testing the Last_value function.

Here is the test script

; withT as(SELECT TOP  -Row_number () Over(ORDER  byCOLUMN_ID) asNum fromsys.columns)SelectNum,num/Ten  asgrp_factor,first_value (num) Over(Partition byNum/Ten Order  byNum asfirst_value,last_value (num) Over(Partition byNum/Ten Order  byNum asLast_value fromT

According to my prediction, 0-9 of First_value will return 1, and Last_value will return 9. But the situation is that First_value did return 1. However, Last_value returns the same value as the NUM field itself. Didn't figure out what was going on. There seems to be no problem with the statement.

SQL Server->> First_value and Last_value functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.