我正在处理一个数据集,其中我希望为每个使用驱动程序SKU销售的class_rk获得#1卖家,然后在每个class_rk中使用#2卖家等.Class_rk(其中已与先前查询中已识别的sold_sku相关联。这是数据集:
driver_sku sold_sku class_rk sold_units
555 432 1 20
555 235 2 17
555 322 2 15
555 253 1 14
555 152 3 13
555 732 1 9
555 913 3 6
我正在寻找的输出:
driver_sku sold_sku class_rk sold_units
555 432 1 20
555 235 2 17
555 152 3 13
555 253 1 14
555 322 2 15
555 913 3 6
555 732 1 9
以下是我迄今为止尝试过的代码:
select driver_sku, sold_sku, class_rk, sold_count,
row_number() over(partition by driver_item_id order by class_rk, sold_count desc) as rank from data_table;
答案 0 :(得分:1)
根据您描述问题的方式,您似乎希望按class_rk
进行分区:
select driver_sku, sold_sku, class_rk, sold_count,
row_number() over(partition by driver_item_id, class_rk
order by sold_count desc
) as rank
from data_table;
但是,这不符合预期的结果。
编辑:
根据您描述问题的方式,您似乎希望按class_rk
进行分区并更改order by
:
select t.*
from (select driver_sku, sold_sku, class_rk, sold_count,
row_number() over (partition by driver_item_id, class_rk
order by sold_count desc
) as rank
from data_table
) t
order by rank, class_rk
如果你真的想要枚举它们:
select t.*,
row_number() over (partition by driver_sku, rank
order by sold_count desc) as newcol
from (select driver_sku, sold_sku, class_rk, sold_count,
row_number() over (partition by driver_item_id, class_rk
order by sold_count desc
) as rank
from data_table
) t
order by rank, class_rk
答案 1 :(得分:0)
根据您的评论,听起来您只想为每个class_rk创建一条记录。我想这可能是你正在寻找的东西:
SELECT driver_sku, sold_sku, class_rk, max(sold_count)
FROM <TABLE>
GROUP BY driver_sku, class_rk
ORDER_BY class_rk
但是如果你想要在你的问题中订购,我认为你必须做这样的事情:
with ranked as (
SELECT driver_sku, sold_sku, class_rk, sold_count,
row_number() over(partition by driver_sku, class_rk order by sold_count desc) as rank
FROM <TABLE>
)
SELECT driver_sku, sold_sku, class_rk, sold_count
FROM ranked
GROUP BY driver_sku, rank
ORDER BY class_rk