语法: ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
方法/步骤
1、简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。?
2、row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)
举例:
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS 序号, --序号排序 B.CXM AS 被检测人姓名,C.CSFZH AS 被检测人身份证号, CASE WHEN C.CLXDH=‘‘ OR C.CLXDH =‘未提供‘ OR C.CLXDH =‘无‘ OR C.CLXDH =‘-‘ THEN C.CJTDH ELSE C.CLXDH END AS 被检测人手机号码, A.CXMJG AS 检测结果, ROW_NUMBER() OVER(PARTITION BY C.CSFZH ORDER BY A.CXMJG DESC) AS 检测批次, --同一个人,出现两次进行排序 CONVERT(VARCHAR,B.DCZRQ,111)+‘ ‘+CONVERT(VARCHAR,B.DCZRQ,108) AS 检测时间,CONVERT(VARCHAR,B.DSHRQ,111)+‘ ‘+CONVERT(VARCHAR,B.DSHRQ,108) AS 检测报告时间 FROM YXLIS2021..VTBJYMXDATA2021 A, YXLIS2021..VTBBRYBXX2021 B ,YSGZZ..TBBRJBXX C WHERE cjyxmbm IN(‘BLK358‘,‘JZSYS310‘,‘JZSYS282‘,‘JZSYS283‘) AND A.CBGDH=B.CBGDH AND B.CBRID=C.CBRID AND B.DSHRQ>=‘2021-08-08 00:00:00.000‘ AND B.DSHRQ<‘2021-08-08 23:59:59.000‘ --更改这个审核日期进行时间段的统计 ORDER BY 序号 ASC--按照审核时间进行排序