ref_number 是xxtable中的一列,它具有多次相同的值。对于每5个值,我需要将别名类型增加1 - 最大类型 100
**Select type,ref_number
from (select '70' type
, ref_number
from xxtable)**
查询结果应如下所示
type ref_number
---- ----------
70 1001
70 1001
70 1001
70 1001
70 1001
71 1001
71 1001
71 1001
71 1001
71 1001
72 1001
72 1001
72 1001
.
.
.
100
答案 0 :(得分:1)
如果我理解正确,你想要这个:
select least(trunc(70+count(*)/5), 100), ref_number
from xxtable
group by ref_number
编辑: 所以,我认为应该有效
select least(trunc(70+(row_number() over (partition by ref_number order by ref_number)-1)/5), 100), ref_number
from xxtable
编辑: 这应该有效:
select type, LISTAGG(ref_number, '') WITHIN GROUP (ORDER BY ref_number)
from(
select least(trunc(70+(row_number() over (partition by ref_number order by ref_number)-1)/5), 100) type, ref_number
from xxtable)
group by type, ref_number
注意:我无法尝试,所以我可能会在那里添加另一个错误。
答案 1 :(得分:0)
如果不同type
ref_number
正在增加,您仍然无法指定。如果没有,user2179887的想法就足够了。如果是,则必须添加另一个逻辑。如果没有多个("嵌套")分析函数的使用,我担心它是不可能的:
with xxtable as (
select column_value as ref_number
from table(sys.KU$_OBJNUMSET(
1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,
1002,1002,1002,
1003,1003,1003,1003,1003,1003,
1004,
1005,1005,1005,1005,1005,1005,
1006
))
)
select
70 + trunc(((row_number() over (partition by x.ref_number order by null)) - 1)/5) as user2179887_corrected_type,
x.ref_number,
70 + trunc(((row_number() over (partition by x.ref_number order by null)) - 1)/5) + nvl(g.gs,0) as increasing_type
from xxtable x
join (
select
ref_number,
sum(trunc((count(*) + 4) / 5))
over (order by ref_number rows between unbounded preceding and 1 preceding) as gs
from xxtable
group by ref_number
) g on g.ref_number = x.ref_number
说明:
with
- 子句仅用于Oracle内置集合中的数据模拟,您可以安全地复制其余的查询ref_number
,g
子选择预先计算所有行的贡献ref_number
到结果type
sum over
收集以前值的这些总和(窗口条款中为1 preceding
)我认为这超出了分析功能的可理解范围。也许使用model
子句可以提供更可读的结果。无论如何,感谢有趣的问题,让我更多地了解分析功能: - )。