clwn.net
当前位置:首页 >> sql 分组取每组第一条 >>

sql 分组取每组第一条

select 星期,max(字母) As 字母 from 表 group by 星期

select * from ( select row_number() over(partition by '分组' order by '日期') as rownum -- 排序并分组 , * -- 所需显示的字段 from 表 ) as T where T.rownum = 1 对每组的数据按日期排序并加上行号 取出时只取行号为1,也就是第一条数据。

用row_number函数可以解决。 1、创建测试表及插入数据: create table test(id int,name varchar(10),score int,classname varchar(20));insert into test values (1,'张三',100,'一班');insert into test values (2,'李四',89,'一班');insert i...

select m, nfrom ( select row_number () over (partition by m order by n desc) rn,--以m分组,分组内以n倒序排列求每组中各自的序号 m, n from table where ...) wwhere w.rn

select 楼栋id,楼栋名称,管理人员联系方式,层高,面积 from ( select 楼栋id,楼栋名称,管理人员联系方式,row_number() over(partition by 楼栋id order by 层高 desc) rn,层高,SUM(建筑面积) OVER(PARTITION BY 楼栋id) as 面积 from dbo.进阶视...

A与max(C)组合后,值是否唯一? 如果唯一可以这样: select A,C,(select B from T t where t.A = K.A AND t.C = K.C) B from ( select t.A A,max(t.C) C from T t group by T.A ) K

select userid,ranking,username from table where userid+ranking in ( select userid+max(ranking) from table group by userid )

请使用max函数

select * from (select name,max(time) time from table_abc gruop by name )b, table_abc where con='1' and b.name=table_abc.name and b.time=table_abc.time

CREATE TABLE test ( channelId int, subChanID INT ); INSERT INTO test SELECT 1, 11 UNION ALL SELECT 1, 12 UNION ALL SELECT 1, 13 UNION ALL SELECT 1, 14 UNION ALL SELECT 1, 15 UNION ALL SELECT 2, 21 UNION ALL SELECT 2, 22 UNION A...

网站首页 | 网站地图
All rights reserved Powered by www.clwn.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com