SELECT TOP 2 A.*
FROM t A
LEFT JOIN t B ON A.emp_no=B.emp_no AND A.emp_date<B.emp_date
WHERE
B.emp_id='00001'
ORDER BY A.emp_date DESC你参考一下吧,返回的结果集表示:第一行是前一条,第二行是前二条,以此类推

解决方案 »

  1.   


    SELECT TOP 2 A.*
    FROM t A
        LEFT JOIN t B ON A.emp_no=B.emp_no AND A.emp_date<B.emp_date
    WHERE
        B.emp_id='00001'
    ORDER BY A.emp_date DESC
    楼上正解
      

  2.   

    select ROW_NUMBER()over(order by emp_date desc)前几条次序,* from t t1 
    where exists(select * from t where emp_id='00003' and t1.emp_no=t.emp_no and t.emp_date>t1.emp_date )你试试,
      

  3.   


    select top 2 * 
      from  T  a
          where  exists (select    * 
      from   T 
          where  
         emp_id='00001'
    and emp_no=a.emp_no
    and emp_date>a.emp_date)结果
    emp_id     emp_no     emp_date
    ---------- ---------- -----------------------
    00002      123        2014-09-14 00:00:00.000
    00003      123        2014-09-01 00:00:00.000(2 行受影响)
      

  4.   

    首先多谢各位。我的描述有点出错,我是想分别用两条sql语句查出前一条,跟再前一条的记录。麻烦各位帮我看看怎么实现?
      

  5.   

    with temp as(
    select ROW_NUMBER()over(order by emp_date desc)orderid,* from t t1 
    where exists(select * from t where emp_id='00001' and t1.emp_no=t.emp_no and t.emp_date>t1.emp_date )
    )
    select emp_id,emp_no,emp_date from temp where orderid=1--前一条
    union all
    select  emp_id,emp_no,emp_date from temp where orderid=2--前二条
      

  6.   

    为啥要分开查找。一个语句都查找出来。然后你用条件判断,第ID=1就是第一条。ID=2就是第二条。
    需要几条就查找几条 多方便