SQL 服务器 - 如何将 RANK 函数插入已按排名顺序排序的行?

SQL Server - How would I insert a RANK function to rows that are already sorted in ranked order?

所以,显然,根据我的教授的说法,我的一切都是正确的,除了一列显示了下面代码中所示列的排名。我在想,本质上,它只需要在其自己的列中将行号显示在左侧。以下是说明:

The sales manager would now like you to create a report that ranks her products by both their total sales and total sales quantity (each will be its own column). Create a stored procedure that returns the following columns but also with the two new rank columns added.

Product Name | Orders Count | Total Sales Value | Total Sales Quantity

我知道它在作业描述中没有那个额外的栏,但我想我需要它。这是我目前所拥有的:

USE OnlineStore
GO

CREATE PROC spManagerProductSalesCount
AS
BEGIN
SELECT 
    P.Name AS 'Product Name',
    Isnull(Count(DISTINCT O.OrderID), 0) AS 'Orders Count',
    Sum(Isnull(O.OrderTotal, 0)) AS 'Total Sales Value',
    Sum (Isnull(OI.OrderItemQuantity, 0)) AS 'Total Sales Quantity'
FROM 
    Product P
INNER JOIN 
    OrderItem OI ON P.ProductID = OI.ProductID
INNER JOIN 
    Orders O on O.OrderID = OI.OrderID
GROUP BY
    P.Name
ORDER BY
    'Total Sales Value' DESC, 'Total Sales Quantity' DESC
END

更新:它确实需要在存储过程中并使用 CTE can/should。我可以在 CTE 方面得到一些帮助。这些对我来说很难。

这只是存储过程的 select 部分,但它应该告诉您要做什么:

declare @products table
(
Name varchar(50),
id int
)
declare @orderitems table
(
id int,
orderid int,
productid int,
orderitemquantity int
)
declare @orders table
(
orderid int,
ordertotal decimal(18,2)
)

insert into @products VALUES ('apple', 1)
insert into @products VALUES ('orange', 2)
insert into @products VALUES ('pear', 3)
insert into @products VALUES ('melon', 4)

insert into @orders values(1, 19.0)
insert into @orders values(2, 25.5)
insert into @orders values(3, 9.5)
insert into @orders values(4, 13.5)
insert into @orders values(5, 8.5)

insert into @orderitems VALUES(1, 1, 1, 20)
insert into @orderitems VALUES(2, 1, 2, 10)
insert into @orderitems VALUES(3, 2, 3, 5)
insert into @orderitems VALUES(4, 2, 4, 4)
insert into @orderitems VALUES(5, 3, 1, 10)
insert into @orderitems VALUES(6, 3, 2, 5)
insert into @orderitems VALUES(7, 4, 3, 3)
insert into @orderitems VALUES(8, 4, 4, 2)
insert into @orderitems VALUES(9, 5, 1, 5)
insert into @orderitems VALUES(10, 5, 4, 2)

;WITH summary as 
(
    SELECT p.Name as ProductName, 
    COUNT(o.orderid) as 'Orders Count',
    ISNULL(Sum(o.ordertotal),0) AS 'Total Sales Value',
    ISNULL(Sum(oi.orderitemquantity),0) AS 'Total Sales Quantity'
    FROM @products p
    INNER JOIN @orderitems oi on oi.productid = p.id
    INNER JOIN @orders o on o.orderid = oi.orderid 
    GROUP BY p.Name
)

SELECT ProductName, [Orders Count], [Total Sales Value], [Total Sales Quantity], 
RANK() OVER (ORDER BY [Total Sales Value] DESC) AS ValueRanking,
RANK() OVER (ORDER BY [Total Sales Quantity] DESC) AS QuantityRanking FROM summary

注意这里的一些事情。可以将此代码剪切并粘贴到 Management Studio 查询 window 和 运行 中。它以一些 table 声明和示例数据的插入开始。当你问一个问题时,如果你做这部分工作总是有用的;如果完成了最无聊的部分,人们更有可能回答!

COUNT()不需要ISNULL保护;它 returns 0,如果没有值。

根据最终数据,您会看到 ValueRanking 和 QuantityRankings 是不同的(我摆弄数据得到这个,只是为了说明这一点)。这意味着最终结果只能按其中之一排序(或者实际上按任何其他列排序 - 排序依据不依赖于排名)。

HTH