sql分组取最大一条
2017-08-28 11:44
302 查看
declare @Tab table (Num int, Name varchar(2), Time DATETIME) insert into @tab select 1 ,'a', '2009/05/01' insert into @tab select 1 ,'a', '2009/05/02' insert into @tab select 1 ,'a', '2009/05/03' insert into @tab select 2 ,'b', '2009/05/04' insert into @tab select 2 ,'b', '2009/05/05' insert into @tab select 3 ,'c', '2009/05/06' insert into @tab select 3 ,'c', '2009/05/07' insert into @tab select 5 ,'e', '2009/05/08' insert into @tab select 1 ,'a', '2009/05/09' insert into @tab select 1 ,'a', '2009/05/10' select * from @Tab t where not exists(select 1 from @Tab where num=t.num and [time]<t.[time]) /* Num Name Time ----------- ---- ----------------------- 1 a 2009-05-01 00:00:00.000 2 b 2009-05-04 00:00:00.000 3 c 2009-05-06 00:00:00.000 5 e 2009-05-08 00:00:00.000 (4 行受影响) */
select * from test where b in (select max(id) from test group by a) 适用于所有数据库: select t1.a,t1.b,t1.c from test t1 inner join (seelct a,max(b) as b from test group by a) t2 on t1.a=t2.a and t1.b=t2.b 适用于所有数据库: select a,b,c from( select a,b,c ,row_number()over(partition by a order by b desc) rn from test ) where rn=1
相关文章推荐
- sql获取分组后取某字段最大一条记录
- sql获取分组后取某字段最大一条记录
- 【Oracle】oracle sql 按某个字段分组然后从每组取出最大的一条纪录
- oracle sql 按某个字段分组然后从每组取出最大的一条纪录
- sql 如何按时间排序 分组 并且取每组时间最大或最小的一条数据
- oracle sql 按某个字段分组然后从每组取出最大的一条纪录
- mysql sql 笔记-获取分组中某个字段的最大的行
- SQL分组取最大
- SQL查询相同记录下日期最大的 一条
- SQLSERVER GROUP BY查询最大一条,并关联当前行其它列
- sql 取出一组中时间最大的一条数据
- SQL数据分组后取最大值或者取前几个值(按照某一列排序)
- 获取分组后取某字段最大的一条记录(求每个类别中最大的值的列表)
- 获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)
- SQL分组求最大值
- 写一条SQL查询出每门科目的最高分,按照科目进行分组
- sql分组查询每组的最大值
- sql语句 用户一天的数据,取最大的一条,再统计
- 关于SQL分组取最大值的几中方式
- SQL 分组后获取其中一个字段最大值的整条记录