22,209
社区成员
发帖
与我相关
我的任务
分享
if OBJECT_ID('tab1') is not null
drop table tab1
go
create table tab1
(
id int primary key identity(1,1),
barcode nvarchar(20),
createdate date,
path nvarchar(20)
)
insert into tab1
select 'barcode1','2014-08-05','无' union all
select 'barcode1','2014-08-10','无' union all
select 'barcode2','2014-08-07','无' union all
select 'barcode3','2014-08-09','无'
--1
select * from
(
select ROW_NUMBER() over( partition by barcode order by createdate desc) as d,* from tab1
)x
where d =1
--2
select a.* from tab1 a
where not exists(select 1 from tab1 b where b.barcode=a.barcode and b.createdate>a.createdate)
SELECT tb1.* FROM tb1
INNER JOIN (SELECT ID,barcode,PATH,MAX(createdate) AS createdate from tb1 GROUP BY ID,barcode,path ) tb2
ON tb1.ID=tb2.ID AND tb1.barcode=tb2.barcode AND tb1.path=tb2.path AND tb1.createdate=rb2.createdate
select a.*
from [表名] a
where not exists(select 1
from [表名] b
where b.barcode=a.barcode
and b.createdate>a.createdate)
上面搞错了,不好意思
select a.* from talbe a
where a.createdate=(select max(createdate)from table
where barcode=a.barcode)
order by id
select a.* from talbe a
where a.barcode=(select max(createdate)from table)
order by id