1

一直在搞乱 Volusion 商店的查询,试图按品牌获得最畅销的 sku……我已经这样做了,但我怎么也只显示前 10 个 PER 品牌……

如果我添加前 10 名,则它只有 10 行期间。

select
    products_joined.ProductManufacturer as brand,
    Sum(OrderDetails.ProductPrice * OrderDetails.Quantity) AS TotalSold,
    OrderDetails.ProductCode as sku
from 
    orderdetails, orders, products_joined
where 
    products_joined.ProductCode = OrderDetails.ProductCode 
    and Orders.OrderID = OrderDetails.OrderID 
    and Orders.OrderDate BETWEEN getdate() - 90 AND getdate()
    and Orders.OrderStatus <> 'Cancelled' 
    and products_joined.ProductManufacturer is not null
group by 
    products_joined.ProductManufacturer, OrderDetails.ProductCode
order by
    products_joined.ProductManufacturer,
    Sum(OrderDetails.ProductPrice*OrderDetails.Quantity) DESC
4

2 回答 2

1

如果 ROW_NUMBER 可用,您也可以使用 CTE 并执行类似的操作。

;WITH cteProductsSold AS (
    SELECT  pj.ProductManufacturer AS brand,
            od.ProductCode AS sku,
            SUM(od.ProductPrice * od.Quantity) AS TotalSold
    FROM    orders o
            INNER JOIN orderdetails od ON od.OrderID = o.OrderID
            INNER JOIN products_joined pj ON pj.ProductCode = od.ProductCode
    WHERE   o.OrderDate BETWEEN GETDATE() - 90 AND GETDATE()
            AND o.OrderStatus <> 'Cancelled'
            AND pj.ProductManufacturer IS NOT NULL

    GROUP BY pj.ProductManufacturer,
            od.ProductCode
), cteProductOrdered AS (
    SELECT *,
            ROW_NUMBER() OVER (PARTITION BY brand ORDER BY TotalSold DESC) Rn
    FROM    cteProductsSold
)
SELECT  brand,
        sku,
        TotalSold
FROM    cteProductOrdered
WHERE   Rn < 11

或者,您可以使用派生表而不是 CTE。

SELECT  brand,
        sku,
        TotalSold
FROM    (   SELECT  *,
                    ROW_NUMBER() OVER (PARTITION BY brand ORDER BY TotalSold DESC) Rn
            FROM    (   SELECT  pj.ProductManufacturer AS brand,
                                od.ProductCode AS sku,
                                SUM(od.ProductPrice * od.Quantity) AS TotalSold
                        FROM    orders o
                                INNER JOIN orderdetails od ON od.OrderID = o.OrderID
                                INNER JOIN products_joined pj ON pj.ProductCode = od.ProductCode
                        WHERE   o.OrderDate BETWEEN GETDATE() - 90 AND GETDATE()
                                AND o.OrderStatus <> 'Cancelled'
                                AND pj.ProductManufacturer IS NOT NULL

                        GROUP BY pj.ProductManufacturer,
                                od.ProductCode
                    ) p
        ) ps
WHERE   Rn < 11
于 2016-06-06T20:29:12.390 回答
1

这也应该有效

select * from (
select
products_joined.ProductManufacturer as brand,
Sum(OrderDetails.ProductPrice*OrderDetails.Quantity) AS TotalSold,
OrderDetails.ProductCode as sku,
row_number() over ( partition by products_joined.ProductManufacturer, OrderDetails.ProductCode order by Sum(OrderDetails.ProductPrice*OrderDetails.Quantity) desc) rowid

from orderdetails, orders, products_joined

where 
products_joined.ProductCode = OrderDetails.ProductCode and
Orders.OrderID = OrderDetails.OrderID and 
Orders.OrderDate BETWEEN getdate() - 90 AND getdate()
AND Orders.OrderStatus <> 'Cancelled' and products_joined.ProductManufacturer is not null
GROUP BY products_joined.ProductManufacturer, OrderDetails.ProductCode
) as x
where rowid < 11
ORDER BY brand,TotalSold DESC
于 2016-06-06T20:45:07.960 回答