表名 Ware 结构如下:
... ID Name(商品名称) BarCode(条码) Use(是否启用) EstablishDate(创建日期) ... ...
1 软广喜 6901028001489 1 2010-9-13 13:25:00
2 冰露水 6920476664541 1 2010-8-20 10:20:30
3 双喜(软) 6901028001489 1 2010-10-13 13:25:00
4 黑妹牙膏 6902226158111 1 2010-10-13 13:30:00
5 双喜牌香烟 6901028001489 1 2012-5-13 13:25:00
6 冰露饮用矿物质水 6920476664541 1 2012-5-14 18:21:00
7 ..... ............ ... ..........
这个表是商场POS系统的资料,由于时间紧迫,直接把一个营业中的超市的资料附加过来了,但是价格很凌乱,为了争取时间很多资料重新做过,但是导致很多商品有重复资料,前台收银一扫描就出现好几个,导致收银员胡乱选择,价格出错。现在想
通过对比BarCode值,找出相同具有相同值的行,并且对比创建日期,在Use列中保留最后一个日期的行为1,其他的只要BarCode值相同的剩余行全部设置为0,也就是停用即可。而条码没有重复的继续保留,不受影响。
返回值应该是:
... ID Name(商品名称) BarCode(条码) Use(是否启用) EstablishDate(创建日期) ... ...
1 软广喜 6901028001489 0 2010-9-13 13:25:00
2 冰露水 6920476664541 0 2010-8-20 10:20:30
3 双喜(软) 6901028001489 0 2010-10-13 13:25:00
4 黑妹牙膏 6902226158111 1 2010-10-13 13:30:00
5 双喜牌香烟 6901028001489 1 2012-5-13 13:25:00
6 冰露饮用矿物质水 6920476664541 1 2012-5-14 18:21:00
7 ..... ............ ... ..........
听说CSDN高手如云,个个都是编程狂人,特此跪求师傅指点。万分感谢,这个是商场正在用的资料,还希望师傅小心为妙,毕竟很多东西难于逆转。谢谢师傅!
------解决方案--------------------
- SQL code
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
------解决方案--------------------
- SQL code
if object_id('[TB]') is not null drop table [TB]gocreate 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 allselect 2,'冰露水',6920476664541,1,'2010-8-20 10:20:30' union allselect 3,'双喜(软)',6901028001489,1,'2010-10-13 13:25:00' union allselect 4,'黑妹牙膏',6902226158111,1,'2010-10-13 13:30:00' union allselect 5,'双喜牌香烟',6901028001489,1,'2012-5-13 13:25:00' union allselect 6,'冰露饮用矿物质水',6920476664541,1,'2012-5-14 18:21:00'select * from [TB]with TTas(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.0003 双喜(软) 6901028001489 0 2010-10-13 13:25:00.0001 软广喜 6901028001489 0 2010-09-13 13:25:00.0004 黑妹牙膏 6902226158111 1 2010-10-13 13:30:00.0006 冰露饮用矿物质水 6920476664541 1 2012-05-14 18:21:00.0002 冰露水 6920476664541 0 2010-08-20 10:20:30.000(6 行受影响)
------解决方案--------------------
- SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]gocreate table [test]([ID] int,[Name] varchar(16),[BarCode] bigint,[Use] int,[EstablishDate] datetime)goinsert [test]select 1,'软广喜',6901028001489,1,'2010-9-13 13:25:00' union allselect 2,'冰露水',6920476664541,1,'2010-8-20 10:20:30' union allselect 3,'双喜(软)',6901028001489,1,'2010-10-13 13:25:00' union allselect 4,'黑妹牙膏',6902226158111,1,'2010-10-13 13:30:00' union allselect 5,'双喜牌香烟',6901028001489,1,'2012-5-13 13:25:00' union allselect 6,'冰露饮用矿物质水',6920476664541,1,'2012-5-14 18:21:00'gowith mas(select * from (selectid,px=row_number()over(partition by [BarCode] order by [EstablishDate] desc),[Name],[BarCode],[Use],[EstablishDate]from test)twhere px<>1)update testset [Use]=0 where ID in(select ID from m)select * from test/*ID Name BarCode Use EstablishDate1 软广喜 6901028001489 0 2010-09-13 13:25:00.0002 冰露水 6920476664541 0 2010-08-20 10:20:30.0003 双喜(软) 6901028001489 0 2010-10-13 13:25:00.0004 黑妹牙膏 6902226158111 1 2010-10-13 13:30:00.0005 双喜牌香烟 6901028001489 1 2012-05-13 13:25:00.0006 冰露饮用矿物质水 6920476664541 1 2012-05-14 18:21:00.000*/