现有表如下 :
id time
1 10
2 10
3 11
4 11
5 13
6 16
要求写查询语句 求出
2 10
4 11
5 13
6 16
既求出不同的套 time行 ,如果time 行相同 取id 大值
id time
1 10
2 10
3 11
4 11
5 13
6 16
要求写查询语句 求出
2 10
4 11
5 13
6 16
既求出不同的套 time行 ,如果time 行相同 取id 大值
MAX(ID) AS ID,
time
FROM tb
GROUP BY time
select * from TB where id in (select max(id) from TB group by [time])
---- 或者 ------
select max(id) AS ID, [time] from TB group by [time] order by id
declare @tb table([id] int,[time] int)
insert @tb
select 1,10 union all
select 2,10 union all
select 3,11 union all
select 4,11 union all
select 5,13 union all
select 6,16select * from @tb t
where not exists(select 1 from @tb where [id]>t.[id] and time=t.[time])
--测试结果:
/*
id time
2 10
4 11
5 13
6 16
*/
select * from t1 as a
where not exists(select * from t1 where time=a.time and id>a.id)