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你参考一下吧,返回的结果集表示:第一行是前一条,第二行是前二条,以此类推
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你参考一下吧,返回的结果集表示:第一行是前一条,第二行是前二条,以此类推
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
楼上正解
where exists(select * from t where emp_id='00003' and t1.emp_no=t.emp_no and t.emp_date>t1.emp_date )你试试,
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 行受影响)
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--前二条
需要几条就查找几条 多方便