SQL查询Volusion中按品牌划分的前十大销售sku

SQL查询Volusion中按品牌划分的前十大销售sku,sql,sum,volusion,Sql,Sum,Volusion,一直在搞乱Volusion商店的查询,试图按品牌获得最畅销的sku。。。。我已经做到了,但我怎么能只展示每个品牌的前10名呢 如果我加上一个前10行,它只是10行周期 select products_joined.ProductManufacturer as brand, Sum(OrderDetails.ProductPrice * OrderDetails.Quantity) AS TotalSold, OrderDetails.ProductCode as sku

一直在搞乱Volusion商店的查询,试图按品牌获得最畅销的sku。。。。我已经做到了,但我怎么能只展示每个品牌的前10名呢

如果我加上一个前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

如果行号可用,您也可以使用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
这应该也行

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

你在用什么?老实说,我对数据库的任何信息都不确定,我必须在本地系统上编写查询并将它们粘贴到web浏览器管理员中才能运行,而且它有限,不能使用变量或临时表,我也不能直接访问它。它作为电子商务平台的一部分被锁定。你能运行一些测试查询吗?例如,选择产品制造商的订单上的行数作为Rn从已加入的产品中选择产品制造商的订单上的行数作为Rn从已加入的产品中选择。-20多年前,在ANSI-92 SQL标准中,这种旧式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用被禁止了77-这是有效的。第二件事多谢各位,现在我要破译它,这样我才能理解我所扮演的角色missing@M21这是一个很好的开始。ROW_NUMBER是一个排名函数,它将为每一行提供一个数字1到多少行,并且它可以在分区组更改时使用分区重置行号。如果按Sku OrderDetails.ProductCode分区,则如果按产品分组\u.ProductManufacturer,OrderDetails.ProductCode