查询中经常遇到这种查询,分组后取每组第一条.分享下一个SQL语句: --根据 x 分组后.根据 y 排序后取第一条 select * from ( select ROW_NUMBER() over(partition by x order by y desc) RowNum ,testTable.* 注:我使用MS SQL 08 R2
查询username,根据fundcode分组,按照date倒序,取date最大的一条数据 select * from ( select username, row_number() over(partition by fundcode, order by date desc) rn from usertable ) t -----------------------------------------------------------------------------感谢打赏!
首先,将按条件查询并排序的结果查询出来. mysql order by accepttime desc; +---------------------+------+-----+ | accepttime | user | job | +---------------------+------+-----+ :: | :: | :: | :: | +---------------------+------+-----+ rows in set 然后,从中分组选出最新一条记录. mysql ord