The SQL statement queries the method of the second record that matches the condition in the table

Source: Internet
Author: User

SQL to create the table used

CREATE TABLE [dbo]. [Emp_pay] (    [EmployeeID] [int] NOT NULL,    [Base_pay] [money] is not NULL,    [commission] [Decimal] (2, 2) is not null) on [PRIMARY ]

The resulting table, and the data in the table:

--Method OneSelect Top 1 *  from(        SELECT TOP 2 *     from [dbo].[Emp_pay]    WHEREBase_pay=  -    Order  byEmployeeIDASC) asA--Note: Be sure to specify an alias after the derived table. Order  byA.employeeidDESC

--Method TwoSelect IDENTITY(int,1,1) ID,*  into#Temp  from [dbo].[Emp_pay] whereBase_pay=  -Select *  from#Temp whereId=2Drop Table#Temp

--Method ThreeSet RowCount 2SELECT *  into#t from [dbo].[Emp_pay] whereBase_pay=  -Set RowCount 1Delete  from#tSelect *  from#tSet RowCount 0Drop Table#t

The SQL statement queries the method of the second record that matches the condition in the table

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.