首页 > 代码库 > 如何查询每个分组中的一条数据

如何查询每个分组中的一条数据

SELECT ROW_NUMBER() OVER(PARTITION BY a.id ORDER BY 排序字段 desc,排序字段 desc)

FROM 表名

 

项目源码:

SELECT *  FROM (SELECT ROW_NUMBER() OVER(PARTITION BY a.id ORDER BY a.point_num desc,a.last_update_date desc) rn,
        a.a_id as aId,a.id as id,q.q_name as qName,a.a_details as aDetails,
        a.point_num as pointNum,u.user_name as userName,a.created_by as createdBy,
        a.creation_date as creationDate,a.last_update_date as lastUpdateDate
        FROM XXUN_QUESTIONS q,xxun_questions_answers a,xxun_user u  where  q.id=a.id and
        a.created_by=u.created_by and a.enable_flag=‘Y‘ order by a.point_num desc,a.last_update_date desc )
        WHERE rn = 1

如何查询每个分组中的一条数据