上帝的问题
/*
原数据
Id Name Addr Email
1 Roboth bj Roboth@163.com
2 Xinsoft sh Xinsoft@163.com
3 LTP gz LTP@163.com
4 Roboth hd Roboth1@163.com
得到
Id Name Addr Email
2 Xinsoft sh Xinsoft@163.com
3 LTP gz LTP@163.com
4 Roboth hd Roboth1@163.com
*/
DECLARE @t table
(
Id int,
[Name] varchar(200),
Addr varchar(200),
Email varchar(200)
)
Insert into @t
select 1,'Roboth','bj','Roboth@163.com'
Union all
select 2,'Xinsoft','sh','Xinsoft@163.com'
Union all
select 3,'LTP','gz','LTP@163.com'
Union all
select 4,'Roboth','hd','Roboth1@163.com'
--method1
select * from @t A
where
1>(
select COUNT(1) from @t B where A.[Name]=B.[Name] and A.ID<B.ID
)
--method2
select * from @t A
where
not exists (
select (1) from @t B where A.[Name]=B.[Name] and A.ID<B.ID
)
--Method3
select * from @t
where id in
(
select MAX(ID) from @t
group by [Name]
)
--Method4
select A.* from @t A
Inner join
(select MAX(ID) as id from @t
group by [Name]) B
on A.id=B.id

浙公网安备 33010602011771号