日期:2014-05-18 浏览次数:20636 次
update t1 set t1.[Use]=0 from Ware t1,(select BarCode,D=max([EstablishDate]) from Ware group by BarCode) t where t1.BarCode=t.BarCode and t1.EstablishDate<>t.D
------解决方案--------------------
if object_id('[TB]') is not null drop table [TB] go create table [TB] (ID int,Name nvarchar(16),BarCode bigint,[Use] int,EstablishDate datetime) insert into [TB] select 1,'软广喜',6901028001489,1,'2010-9-13 13:25:00' union all select 2,'冰露水',6920476664541,1,'2010-8-20 10:20:30' union all select 3,'双喜(软)',6901028001489,1,'2010-10-13 13:25:00' union all select 4,'黑妹牙膏',6902226158111,1,'2010-10-13 13:30:00' union all select 5,'双喜牌香烟',6901028001489,1,'2012-5-13 13:25:00' union all select 6,'冰露饮用矿物质水',6920476664541,1,'2012-5-14 18:21:00' select * from [TB] with TT as( select ROW_NUMBER() over(partition by barcode order by establishdate desc) as num, * from TB) select ID,name ,barcode,case when num = 1 then 1 else 0 end as [use],establishdate from TT /* ID name barcode use establishdate ----------- ---------------- -------------------- ----------- ----------------------- 5 双喜牌香烟 6901028001489 1 2012-05-13 13:25:00.000 3 双喜(软) 6901028001489 0 2010-10-13 13:25:00.000 1 软广喜 6901028001489 0 2010-09-13 13:25:00.000 4 黑妹牙膏 6902226158111 1 2010-10-13 13:30:00.000 6 冰露饮用矿物质水 6920476664541 1 2012-05-14 18:21:00.000 2 冰露水 6920476664541 0 2010-08-20 10:20:30.000 (6 行受影响)
------解决方案--------------------
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] go create table [test]( [ID] int, [N