语法: ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
举例:
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--按照审核时间进行排序
原文:https://www.cnblogs.com/since-1995/p/15130025.html