1、ROW_NUMBER()函数:根据f_id升序排序(ASC)后的记录,为每条记录增添递增的顺序数值序号(1、2、3……),即使f_id的值相同也递增;
SELECT ROW_NUMBER() OVER (ORDER BY f_name ASC) AS ROWID,f_name,f_id FROM fruits;
SELECT ROW_NUMBER() OVER (ORDER BY CASE WHEN f_name IS NULL THEN 2 ELSE 1 END ASC) AS ROWID,f_name,f_id FROM fruits;--f_name为NULL值时排到后面;
2、RANK()函数:与ROW_NUMBER()不同的是,f_id值相同,对应的顺序数值也相同(比如,如果三人的分数相同,则他们三并列第一,而第四位的排名则是第四);
SELECT RANK() OVER (ORDER BY f_name ASC) AS ROWID,f_name,f_id FROM fruits;
3、DENSE_RANK()函数:与ROW_NUMBER()不同的是,f_id值相同数值序号也相同,接下来继续递增;
SELECT DENSE_RANK() OVER (ORDER BY f_name ASC) AS DENSEID,f_name,f_id FROM fruits;
4、NTILE()函数:将记录分成指定的组数;
SELECT NTILE(3) OVER (ORDER BY f_name ASC) AS DENSEID,f_name,f_id FROM fruits;
SELECT NTILE(4) OVER (ORDER BY f_name ASC) AS DENSEID,f_name,f_id FROM fruits;
SELECT NTILE(6) OVER (ORDER BY f_name ASC) AS DENSEID,f_name,f_id FROM fruits;