为什么Rank()不能正常工作?

时间:2010-07-27 16:54:28

标签: sql-server-2008

这是我的表数据: -

Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Lux','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Crowning Glory','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (2,'Cinthol','nice soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (3,'Lux','nice soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (3,'Lux','nice soap','soap');

当我发出此查询时: -

Select  ProductId, 
        ProductName, 
        Description,
        Category, 
        RANK() Over ( partition by ProductId Order By ProductId) As Rank
From tblProduct;

我所能看到的每一行都有1st等级。所有其他其他职级在哪里?由于我没有使用2,因此productid 6的行应该具有等级DENSE_RANK()。为什么查询不起作用?

1 个答案:

答案 0 :(得分:3)

您正在按ProductId进行分区 - 这意味着每个“分区”将从1开始变换。尝试删除PARTITION BY ProductId