如何只取出一条规定条件相同取ID号大的数据
有一表比如
ID H T V
1 1 1 1
2 1 1 2
1 2 1 2
1 3 2 1
2 3 3 3
2 2 1 4
如何通过SQL语句 取出数据 要求 如何 H 和 T 都相同 并且 ID 不同的情况下 只取出ID大的数据
[解决办法]
select * FROM table as T
left outer jion
(select Max(a.id) as id ,a.h,a.t froM table as a
where (select * FROM table as b where a.h=b.h and a.t=b.t and a.id<>b.id )
group by a.h,a.t) as C
where T.h = c.h and T.T = C.T
[解决办法]
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([ID] int,[H] int,[T] int,[V] int)insert [tb]select 1,1,1,1 union allselect 2,1,1,2 union allselect 1,2,1,2 union allselect 1,3,2,1 union allselect 2,3,3,3 union allselect 2,2,1,4goselect * from tb twhere not exists(select 1 from tb where h=t.h and t=t.t and id>t.id)/**ID H T V----------- ----------- ----------- -----------2 1 1 21 3 2 12 3 3 32 2 1 4(4 行受影响)**/