1

我有一个查询返回包含客户名字和姓氏的 2 列的总和。

它返回大约 40000 条记录。我的查询是:-

SELECT  SUM(Orders.BusinessVolumeTotal) AS BV,
        SUM(Orders.CommissionableVolumeTotal) AS PV,
        ISNULL(Customers.FirstName,''), Customers.LastName
FROM    Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE   Orders.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
        AND Orders.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
GROUP BY Customers.FirstName, Customers.LastName

但我只想返回 BV 和 PV 列中值最高的 10 行。

提前致谢。

4

3 回答 3

2

如果您想要每组 10 个最高的行,您可以ROW_NUMBER在 a中使用CTE

WITH CTE AS
(
    SELECT SUM(Orders.BusinessVolumeTotal) OVER(PARTITION BY Customers.FirstName, Customers.LastName) AS BV, 
           SUM(Orders.CommissionableVolumeTotal) OVER(PARTITION BY Customers.FirstName, Customers.LastName) AS PV, 
           ISNULL(Customers.FirstName,'') As FirstName, 
           Customers.LastName,
       RN = ROW_NUMBER() 
              OVER(PARTITION BY Customers.FirstName, Customers.LastName
                   ORDER BY (Orders.BusinessVolumeTotal + Orders.CommissionableVolumeTotal) DESC) 
    FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
    WHERE  Orders.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
    AND Orders.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')        
)
SELECT * FROM CTE WHERE RN <= 10
于 2013-10-28T12:43:18.250 回答
1

基于两列的组,您可以获得前 10 行,如下所示:

SELECT  TOP 10
        SUM(Orders.BusinessVolumeTotal) AS BV,
        SUM(Orders.CommissionableVolumeTotal) AS PV,
        ISNULL(Customers.FirstName,''), Customers.LastName
FROM    Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE   Orders.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
        AND Orders.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
GROUP BY Customers.FirstName, Customers.LastName
ORDER BY SUM(Orders.BusinessVolumeTotal) + SUM(Orders.CommissionableVolumeTotal) DESC
于 2013-10-28T12:50:00.480 回答
0

获得 BV 前 10 名和 PV 前 10 名的一种方法是为每个查询编写一个查询:

SELECT TOP 10
SUM(Orders.BusinessVolumeTotal) AS BV, SUM(Orders.CommissionableVolumeTotal) AS PV, ISNULL(Customers.FirstName,''), Customers.LastName
FROM         Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
 WHERE  Orders.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND Orders.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                    GROUP BY Customers.FirstName, Customers.LastName
ORDER BY BV DESC

SELECT TOP 10
SUM(Orders.BusinessVolumeTotal) AS BV, SUM(Orders.CommissionableVolumeTotal) AS PV, ISNULL(Customers.FirstName,''), Customers.LastName
FROM         Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
 WHERE  Orders.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND Orders.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                    GROUP BY Customers.FirstName, Customers.LastName
ORDER BY PV DESC
于 2013-10-28T12:36:37.000 回答