SQL查询查看表的前两条记录

时间:2014-11-28 21:19:42

标签: sql sql-server-2012

我有一份员工表来了解薪水/费率历史记录。它看起来像......

Employee#    EffectDate     Salary
00016        2014-01-03     78100.00
00016        2013-07-03     75130.00
00016        2013-01-06     72140.00
00114        2014-07-15     85610.00
00244        2014-01-10     54130.00
00244        2013-06-30     50140.00
00634        2013-12-25     72560.00
00634        2013-04-05     69348.00
00634        2012-01-01     64530.00

我正在尝试为员工提供一行显示当前薪水,上一个生效日期和即时上一个薪水......

Employee#    CurrentSalary   LastChange    PreviousSalary
00016        78100.00        2014-01-03    75130.00
00114        85610.00        2014-07-15    NULL
00244        54130.00        2014-01-10    50140.00
00634        72560.00        2013-12-25    69348.00

使用Microsoft SQL 2012

3 个答案:

答案 0 :(得分:0)

从头顶快速变化:

select
    t.Employee,
    (select top 1 Salary
        from table as a
        where a.Employee = t.Employee and a.EffectDate = t.LastChange
        order by EffectDate desc) as CurrentSalary,
    t.LastChange,
    (select top 1 Salary
        from table as a
        where a.Employee = t.Employee and a.EffectDate < t.LastChange
        order by EffectDate desc) as PreviousSalary
from (
    select Employee, max(EffectDate) as LastChange
    from table
    group by Employee) as t

答案 1 :(得分:0)

此处使用row_numbermaxcase(一种旋转形式)的一种方式:

with cte as (
  select *,
    row_number() over (partition by employeenum order by effectdate desc) rn
  from salary
  )
select employeenum,
  max(case when rn = 1 then salary end) currentsalary,
  max(case when rn = 1 then effectdate end) lastchange,
  max(case when rn = 2 then salary end) previoussalary
from cte
group by employeenum

答案 2 :(得分:0)

尝试使用CTE,您可以通过分区获得ROW_NUMBER(),然后在查询中使用此行号。

DECLARE @tbl TABLE (
    EmployeeId    VARCHAR (20)  ,
    EffectiveDate DATE          ,
    salary        DECIMAL (8, 2));

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00016, getdate(), 7852.00);

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00016, getdate() + 1, 7952.00);

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00016, getdate() + 2, 8052.00);

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00017, getdate(), 7852.00);

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00017, getdate() + 1, 7952.00);

INSERT  INTO @tbl (EmployeeId, EffectiveDate, salary)
VALUES           (00018, getdate(), 7852.00);

WITH   cteSalary
AS     (SELECT EmployeeId,
               EffectiveDate,
               salary,
               ROW_NUMBER() OVER (PARTITION BY EmployeeId 
                              ORDER BY EffectiveDate DESC) AS rn
        FROM   @tbl)
SELECT a.EmployeeId,
       a.EffectiveDate,
       a.salary,
       b.salary AS 'Previous salary'
FROM   cteSalary AS a
       LEFT OUTER JOIN
       cteSalary AS b
       ON a.EmployeeId = b.EmployeeId
          AND b.rn = 2
WHERE  a.rn = 1;