ROW_NUMBER、RANK()、DENSE_RANK()和OVER的使用
/*以FoodPrice列排序并显示排序后的行号*/ SELECT ROW_NUMBER() OVER(ORDER BY FoodPrice DESC) AS RowId,* FROM dbo.Food
ROW_NUMBER()
/*以FoodType分组,然后以FoodPrice排序并显示所在分组的相应行号*/ SELECT ROW_NUMBER() OVER(PARTITION BY FoodType ORDER BY FoodPrice DESC) AS RowId,* FROM dbo.Food
RANK()
/*以FoodType分组,然后以FoodPrice排序并显示所在分组的相应行号,如果排序字段字相等则显示相同的行号, 并跳过所在的真实行号*/ SELECT RANK() OVER(PARTITION BY FoodType ORDER BY FoodPrice) AS RowID,* FROM dbo.Food
DENSE_RANK()
/*以FoodType分组,然后以FoodPrice排序并显示所在分组的相应行号,如果排序字段字相等则显示相同的行号, 忽略相同行号,依次递增*/ SELECT DENSE_RANK() OVER(PARTITION BY FoodType ORDER BY FoodPrice) AS RowID,* FROM dbo.Food