以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  一个数据库表,有重复值,取最新,其他的删掉  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=141008)

--  作者:fubblyc
--  发布时间:2019/9/19 11:46:00
--  一个数据库表,有重复值,取最新,其他的删掉
老师,一个数据库表
条码货号,字段:

条码     货号    更新时间
1111    101    2015-01-01
1111    105    2016-02-03
1111    112    2019-09-18


2015,2016的的条码已经不用,要删掉,只留下最新的时间

这种sql语句要怎么写呢?

Dim cmd As new SQLCommand
cmd.C
cmd.CommandText = " delet e from 条码货号 where  Max(更新时间)  "


这个有点复杂~~



--  作者:有点蓝
--  发布时间:2019/9/19 13:47:00
--  
试试

delete from 条码货号 where 
[_Identify] in ( select [_Identify] from 条码货号 as b inner join (selec条码,max(更新时间) as 更新时间 from 条码货号  group by 条码 having count(条码) > 1) as a on a.条码 = b.条码 and a.更新时间 <> b.更新时间)
--  作者:fubblyc
--  发布时间:2019/9/21 10:49:00
--  
非常快,非常感谢蓝大大