【问题标题】:Using Rank function in Join在 Join 中使用 Rank 函数
【发布时间】:2023-04-03 07:27:01
【问题描述】:

我正在使用下面的 oracle 代码来查找售出商品最多的前 5 家商店。这 5 家商店之后的任何东西都应该组合到一个“其他”存储桶等级 6 中。Run_id 和scenario_ID 具有一对一的关系。当只有一个run_id 和一个scenario_id 时,此代码有效。当我只有一个run_id 和一个scenario_id 时,这段代码效果很好。但是,为了提高效率,我想仅在查询时为每个场景提升前 5 名。当我这样做时,第一个 run_id 将显示前 5 个链和所有其他存储桶,之后的每个 run_id 只显示排名为 6 的所有其他存储桶。

SELECT c.SCENARIO_ID, case when C.YEAR_MONTH_NBR >= to_char(r.proj_start_dt,'YYYYMM') then 'G' else 'L' End as overUnd, case when rnk <= 5 then c.model_nme else 'Other' end as Chain_Nme, sum(c.item_qty), case when rnk <= 5 then rnk else 6 end as final_rank
  from USR.pdr_summary c
  Left Outer Join run_tbl r on c.run_id = r.run_id
  Left join (SELECT c.scenario_id, c.model_nme, rank () over (order by c.scenario_id, sum(C.item_qty) desc) as Rnk
     from USR.pdr_summary c
     where c.run_id IN ('1110','1111','1112','1113')
         and c.scenario_id IN('5423','5424','5425','5426')
         and c.model_nme <> 'Other Retail'
     group by c.scenario_id, c.model_nme) rank_data
     on rank_data.model_nme = c.model_nme
      and rank_data.scenario_id = c.scenario_id
     where c.run_id IN ('1110','1111','1112','1113')
         and c.scenario_id IN('5423','5424','5425','5426')
  group by c.SCENARIO_ID, case when C.YEAR_MONTH_NBR >= to_char(r.proj_start_dt,'YYYYMM') then 'G' else 'L' End, case when rnk <= 5 then c.model_nme else 'Other' end, case when rnk <= 5 then rnk else 6 end
     order by c.SCENARIO_ID, overUnd, case when rnk <= 5 then rnk else 6 end asc

当我从排名数据查询中删除 and rank_data.scenario_id = c.scenario_id 行时,在某些情况下我会得到所有 6 个排名,而在其他情况下我会得到排名 1、3、5 和 6 或 1、2、4 和 6。

我认为这是我进行连接的方式的问题,但我不知道为什么?

我想要的结果如下:

Scenario_id OverUnd Chain_nme   item_qty    rank
5423    G   Walmart        1000     1
5423    G   Amazon          950     2
5423    G   Target      750     3
5423    G   Walgreens   600     4
5423    G   CVS     500     5
5423    G   Other       800     6
5423    L   Trader Joe  1000        1
5423    L   Amazon          950     2
5423    L   Target      750     3
5423    L   Walgreens   600     4
5423    L   Walmart         500     5
5423    L   Other       800     6
5424    G   Walgreens   1000        1
5424    G   Amazon          950     2
5424    G   Target      750     3
5424    G   Walmart         600     4
5424    G   CVS     500     5
5424    G   Other       800     6
5424    L   Trader Joe  1000        1
5424    L   Amazon          950     2
5424    L   Target      750     3
5424    L   Walgreens   600     4
5424    L   Walmart         500     5
5424    L   Other       800     6

我得到的是:

Scenario_id OverUnd Chain_nme   item_qty    rank
5423        G       Walmart 1000        1
5423        G       Amazon  950     2
5423        G       Target      750     3
5423        G       Walgreens   600     4
5423        G       CVS     500     5
5423        G       Other       800     6
5423        L       Trader Joe  1000        1
5423        L       Amazon  950     2
5423        L       Target      750     3
5423        L       Walgreens   600     4
5423        L       Walmart 500     5
5423        L       Other       800     6
5424        G       Other       700     6
5424        L       Other       900     6
5425        G       Other       700     6
5425        L       Other       900     6
5426        G       Other       700     6
5426        L       Other       900     6

抱歉,当我复制/粘贴数据时,我不确定如何使它看起来像一个表格。

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • @GordonLinoff 我添加了我的结果以及我希望它的样子。

标签: sql oracle plsql ranking


【解决方案1】:

有人通过电子邮件向我发送了我的问题的答案。我需要按scenario_id 和item_qty 进行排名。

(partition by c.scenario_id order by c.scenario_id, sum(C.item_qty) desc) as Rnk

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2016-10-14
    相关资源
    最近更新 更多