日期:2014-05-16 浏览次数:20722 次
if object_id('[NTB]') is not null drop table [NTB]
create table [NTB] (SN int,ITEM varchar(3),日期 datetime)
insert into [NTB]
select 123,'XXX','2013-01-01' union all
select 123,'BBB','2014-01-01' union all
select 345,'CCC','2015-01-01'
select * from [NTB]
UPDATE NTB
SET item='111'
WHERE sn=123 AND 日期 IN (SELECT TOP 1 日期 FROM NTB WHERE sn=123 ORDER BY 日期 DESC)
/*
SN ITEM 日期
123 XXX 2013-01-01 00:00:00.000
123 111 2014-01-01 00:00:00.000
345 CCC 2015-01-01 00:00:00.000*/
UPDATE A SET ITEM = XXXX
FROM (SELECT TOP 1 SN, 日期 FROM A WHERE SN = '123' ORDER BY 日期 DESC) B
WHERE A.SN = B.SN
AND A.日期 = B.日期