一.问题 groupBY分组后取最新一条记录的SQL的解决方案. 二.解决方案 select Message,EventTime from PT_ChildSysAlarms as a where EventTime = (select max(b.EventTime) from PT_ChildSysAlarms as b where a.PtName = b.PtName ) group by Message,EventTime order by EventTime desc
查询中经常遇到这种查询,分组后取每组第一条.分享下一个SQL语句: --根据 x 分组后.根据 y 排序后取第一条 select * from ( select ROW_NUMBER() over(partition by x order by y desc) RowNum ,testTable.* 注:我使用MS SQL 08 R2
在hibernate框架和mysql.oracle两种数据库兼容的项目中实现查询每个id最新更新的一条数据. 之前工作中一直用的mybatis+oracle数据库这种,一般写这类分组排序取每组最新一条数据的sql都是使用row_number() over()函数来实现 例如: select t1.* from ( select t.*, ROW_NUMBER() over(partition t.id order by t.update_time desc) as rn from tab
之前在一个项目的开发中,有遇到要根据分类来分组获取每组一条按某个条件字段排序的数据结果,于是先自己写了一条语句: select * from `表A` GROUP BY `c`; 上面这个语句有可以根据分类分组获得数据,但是无法对获得的数据进行排序,so 继续完善: select * from `表A` where `del`=0 and `markbok`=1 and `id` in(select SUBSTRING_INDEX(group_concat(`id` order by `add_
用Grid显示数据后,如何让系统自动选取第一条记录呢?在显示Grid时由于其Store正在loading,没法在Grid选取第一条记录,因为还没有记录,所以应在其Store进行操作. 查看Ext.data.Store的load()方法如下: load( [options] )Loads data into the Store via the configured proxy. This uses the Proxy to make an asynchronous call to whatever
首先,将按条件查询并排序的结果查询出来. mysql order by accepttime desc; +---------------------+------+-----+ | accepttime | user | job | +---------------------+------+-----+ :: | :: | :: | :: | +---------------------+------+-----+ rows in set 然后,从中分组选出最新一条记录. mysql ord
查询username,根据fundcode分组,按照date倒序,取date最大的一条数据 select * from ( select username, row_number() over(partition by fundcode, order by date desc) rn from usertable ) t -----------------------------------------------------------------------------感谢打赏!
--> 生成测试数据: #TIF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)INSERT INTO #T SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALLSELE