【问题标题】:Enumerate and add in a column of the select - Oracle枚举并在选择的列中添加 - Oracle
【发布时间】:2018-08-17 16:58:01
【问题描述】:

我需要按类型在选择的列中枚举我的行。例如,我有三种类型,可以是“RR”、“RJ”和“RA”,每次出现“RR”类型时,我必须求和 3,而其他类型我必须只求和 1,创建一个序列喜欢:

型号
RR 3
RR 6
RJ 7
RR 10
RJ 11
RA 12
RR 15

我在选择中还有其他字段,所以我使用了 ROW_NUMBER() 函数和我的所有 order by 字段,例如:

select
   number,
   [...]
   type,
   ROW_NUMBER() OVER (order by number, type [...] )*3 as sequence
from
   my_table
order by number, type [...] 

我也尝试使用 case 语句,但它不会聚合值。

有可能吗?我正在尝试使用ROW_NUMBER() 函数,但我无法得到结果,只有三乘三。

【问题讨论】:

    标签: oracle select enumerate line-numbers


    【解决方案1】:

    你可以使用SUM:

    select
       number,
       [...]
       type,
       SUM(CASE WHEN Type = 'RR' THEN 3 ELSE 1 END) 
           OVER (order by number, type [...] ) as sequence
    from my_table
    order by number, type [...] 
    

    Rextester Demo

    【讨论】:

    • 非常感谢!我试过求和,但我没有使用“OVER (ORDER ...”子句。我也不知道这个“rextester”,太棒了!
    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 2022-07-05
    • 1970-01-01
    • 2021-11-19
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多