SQL语句帮助请求

问题描述:

我正在使用Adventureworks2012数据库学习SQL。SQL语句帮助请求

我想创建一个查询来找到所有有加薪的员工。 以下是我得到的结果的前6行:

第一列是businessentityid和加注日期。

BID Salary Raise Date 
4 05.01.2002 00:00:00 
4 01.07.2004 00:00:00 
4 15.01.2006 00:00:00 
16 20.01.2002 00:00:00 
16 16.08.2003 00:00:00 
16 01.06.2006 00:00:00 

随着下面的语句:

select 
    RateChangeDate, Rate, BusinessEntityID 
from 
    HumanResources.EmployeePayHistory 
where 
    BusinessEntityID in (select BusinessEntityID 
         from HumanResources.EmployeePayHistory 
         group by BusinessEntityID 
         having count (*) > 1); 

现在我想要做什么是删除第一行的每一个结果,因为这是当一个人被雇佣了第一份工资,而不是加薪。

此表唯一额外的列是Rate它显示每个日期的费率。

enter image description here

图片是与所述速率列加入到上述声明。

谢谢:)

如果你希望所有但对雇员的第一份薪水,那么你应该学习如何使用窗口功能。如果你正在学习SQL,我会建议你快速学习它们,因为它们非常强大和有用。

你做你想要使用的是什么ROW_NUMBER()

select RateChangeDate, Rate, BusinessEntityID 
from (select eph.*, 
      row_number() over (partition by BusinessEntityID order by RateChangeDate desc) as seqnum 
     from HumanResources.EmployeePayHistory eph 
    ) eph 
where seqnum > 1; 

你可以阅读SQL Server文档中关于row_number()