我在BQ中有销售数据,其中包含以下列 -
Customer Id | ItemID | Order_Qty | Order_Date
我想根据商品的总销售量创建销售排名表。
销售商品的最高排名。
EG。 - 售出总共30个数量的物品abc应该排名1,XYZ项目总共20个销量应该排名2,依此类推。
在我的结果表中,我想要关注列。
ItemId | Total Quantity | Rank
以下是我在遗留SQL格式中的尝试 -
select ItemId ,
RANK() over (partition by ItemId order by Qty desc) as salesrank
from (SELECT ItemId, sum(order_qty) as Qty
FROM sales
group by 1
)
由于某种原因,所有销售排名都是1
答案 0 :(得分:2)
以下是BigQuery Standard SQL
#standardSQL
SELECT
ItemID, Total_Quantity,
RANK() OVER(ORDER BY Total_Quantity DESC) AS rnk
FROM (
SELECT ItemID, SUM(Order_Qty) AS Total_Quantity
FROM sales
GROUP BY ItemID
)
-- ORDER BY rnk
答案 1 :(得分:0)
我猜测你想要排名ean
。如果是这样的话:
SELECT ean, SUM(order_qty) as Qty,
RANK() OVER (ORDER BY SUM(order_qty) DESC) as rnk
FROM sales
GROUP BY ean;
在旧版SQL中,可能是:
SELECT ean, SUM(order_qty) as Qty,
RANK() OVER (ORDER BY qty DESC) as rnk
FROM sales
GROUP BY ean