兄弟们问个 sql 合并相同ID 取最后时间表 A
ID TIME
1 2010-2-6 9:58:52
1 2011-3-8 10:08:52
2 2000-2-6 9:58:52
2 2001-3-11 11:08:52
3 2009-5-22 13:08:52
查询得到
ID TIME
1 2011-3-8 10:08:52
2 2001-3-11 11:08:52
3 2009-5-22 13:08:52
ID TIME
1 2010-2-6 9:58:52
1 2011-3-8 10:08:52
2 2000-2-6 9:58:52
2 2001-3-11 11:08:52
3 2009-5-22 13:08:52
查询得到
ID TIME
1 2011-3-8 10:08:52
2 2001-3-11 11:08:52
3 2009-5-22 13:08:52
from ta t
where time=(select max(time) from ta where id=t.id )
from a
group by ID
go
create table [tA] (ID int,TIME datetime)
insert into [tA]
select 1,'2010-2-6 9:58:52' union all
select 1,'2011-3-8 10:08:52' union all
select 2,'2000-2-6 9:58:52' union all
select 2,'2001-3-11 11:08:52' union all
select 3,'2009-5-22 13:08:52'
select *
from ta t
where time=(select max(time) from ta where id=t.id )
order by id
/*
ID TIME
----------- -----------------------
1 2011-03-08 10:08:52.000
2 2001-03-11 11:08:52.000
3 2009-05-22 13:08:52.000(3 個資料列受到影響)
*/
from tb
from A
group by ID