利用partition分组查询 查询每个分组第一条数据
数据库:sqlserver (其他数据库没有尝试)
row_number() over(partition by year(createtime),month(createtime),day(createtime),glsort ORDER BY createtime DESC)
over(partition by 分组条件)
row_number() over(partition by year(createtime),month(createtime),day(createtime),glsort ORDER BY createtime DESC)
over(partition by 分组条件 order by 排序条件(必须))
select * from ( select glsort,createtime, sort,id, row_number() over(partition by year(createtime),month(createtime),day(createtime),glsort ORDER BY createtime DESC) as new_index from BusinessInformation where isExamine=1 ) a where a.new_index = 1 ORDER BY a.sort desc, year(a.createtime)desc ,month(a.createtime) desc,day(a.createtime) desc