分组行的排名 [英] Ranking for grouped rows

查看:69
本文介绍了分组行的排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含以下字段的表格:
条形码,部门,描述,数量

我设法将数据分组表和显示ReportViewer中的分组数据(按条形码分组并返回以下字段:条形码,部门,描述,总和(数量))。我按Sum(Qty)对返回的数据进行了排序。

我希望将排名放在表格的第一列,就在条形码字段之前。我的问题是,如果我使用= RowNumber(),它总是会返回错误的排名数。

任何人都可以给我一个提示我该怎么做?

谢谢,

Hi all,

I have a table with the following fields:
Barcode, Department, Description, Qty

I managed to group the data in this table and show the grouped data in ReportViewer (group by Barcode and return the following fields: Barcode, Department, Description, Sum(Qty)). I sorted the returned data by Sum(Qty).

I want to put the ranking in the first column of the table, right before the Barcode field. My problem is, if I use =RowNumber(), it always returns incorrect ranking numbers.

Anyone can give me a hint how can I do that?

Thanks,

推荐答案

嘿John.Iam,

你尝试过使用等级功能吗? http://msdn.microsoft.com/en-us/library/ms176102.aspx   ;有文档。

----------------------------------
选择i.ProductID ,p.Name,i.LocationID,i.Quantity
    ,RANK()OVER
    (由i.Quantity DESC订购i.LocationID订单)作为'RANK'从Production.ProductInventory i
    INNER JOIN Production.Product p
        ON i.ProductID = p.ProductID
ORDER BY p.Name;
---------------------------- ---
示例中的示例代码...

这有点棘手,因为您想要在ORDER BY子句中添加要按等级排序的实际列。试一试。让我知道它是怎么回事。
Hey John.Iam,

Have you tried using the rank functionality? http://msdn.microsoft.com/en-us/library/ms176102.aspx There's the documentation.

----------------------------------
SELECT i.ProductID, p.Name, i.LocationID, i.Quantity
    ,RANK() OVER
    (PARTITION BY i.LocationID ORDER BY i.Quantity DESC) AS 'RANK'
FROM Production.ProductInventory i
    INNER JOIN Production.Product p
        ON i.ProductID = p.ProductID
ORDER BY p.Name;
-------------------------------
There's the sample code from the example...

It's a little tricky because you want to add the actual column you want ranked by in the ORDER BY clause. Give that a shot. Let me know how it goes for you.


这篇关于分组行的排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆