首页 > 代码库 > 去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

关键字  ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 

   按照分组字段进行排序并标编号

ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

语法

ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN

 

实例

SELECT XMJL_UID,XMJL,XMCREATETIME,ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN 
FROM SHZT_XMGL WHERE XMSTATUS=进行中 ORDER BY XMCREATETIME

技术分享

 

SELECT * FROM (
SELECT  XMJL_UID,XMJL,XMCREATETIME,ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN   
FROM G_INFOS WHERE  B.XMSTATUS=进行中 ORDER BY XMCREATETIME
) WHERE RN=1

 技术分享

 

去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN