sql 分组后显示每组的前几条记录】的更多相关文章

sql 分组后显示每组的前几条记录 如   表中记录是             code       serialno             A1               1             A1               2             A1               3             A1               5                 B1               2             B1               3…
转自:http://blog.163.com/jeson_lwj/blog/static/135761083201052411115783/ --查询每门课程的前2名成绩 CREATE TABLE StudentGrade( stuId CHAR(4), --学号 subId INT, --课程号 grade INT, --成绩 PRIMARY KEY (stuId,subId) ) GO --表中数据如下 INSERT INTO StudentGrade(stuId,subId,grade)…
使用子查询进行查询 SELECT * FROM home_content a WHERE ( SELECT count(id) FROM home_content WHERE class_link = a.class_link AND id > a.id ) <= 2…
1.取时间最新的记录 不分组有重复(多条CreateTime一样的都是最新记录) select * from test t where pid in ( select PId from Test t where time=(select max(time) from Test t1 where t1.PId=t.PId) group by Pid ) and time=(select max(time) from Test t1 where t1.PId=t.PId) 2.分组后取时间最新的记录…
端午有人休息,有人忙 操作前数据: --从排序后的结果集中删除 前n条记录delete from emp where empno in (select empno                   from (select *                           from emp                          where comm is not null                            and rownum < &n        …
一.对分组的记录取前N条记录:例子:取前 2条最大(小)的记录 .用子查询: SELECT * FROM right2 a WHERE > (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account>a.account) ORDER BY a.id,a.account DESC .用exists半连接: SELECT * FROM right2 a WHERE EXISTS (SELECT COUNT(*) FROM right2…
需要在mysql中解决记录的分组统计.排序,并抽取前10条记录的功能.现已解决,解决方案如下: 1)表结构 CREATE TABLE `policy_keywords_rel` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID', `content_id` int(11) NOT NULL COMMENT '文章id', `keyword_id` int(11) NOT NULL COMMENT '关键词id', `cnt` int(11)…
当前时间:NOW() 前n天:DATE_SUB(NOW(),INTERVAL n DAY) 后n天:DATE_SUB(NOW(),INTERVAL -n DAY) 取前n条记录:SELECT * FROM 表名 LIMIT n 从第n条开始取m条:SELECT * FROM 表名 LIMIT n,m 注释:n从0开始计…
场景:sql server 2008 drop table ID CREATE TABLE ID ( id ,) not null, code int , D date, PRIMARY KEY (id) ) ,getdate()) ,getdate()) ,getdate()) ,getdate()) ,'2017-08-02') ,'2017-08-01') select * from ID 目标: select COUNT(*) from ID group by code 产生code列唯…
select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num,max(dt) dt from test group by gr ) b //如果有重复项,可用如下语句(针对Mysql的limit,Oracle 可用 rownum<2) select gr,num,dt,(select bys from test where gr=b.gr and dt=b.d…