我有这张table:
ELEMENTO VALOR
-------------------------
ELEMENT1_SUFFIX1 2
ELEMENT1_SUFFIX2 4
ELEMENT2_SUFFIX1 5
ELEMENT2_SUFFIX2 15
我想通过划分每对元素和后缀来生成新条目。对于元素的每个划分,我想划分后缀2/后缀1并生成一个具有element_result的条目。如下所示。
ELEMENTO VALOR
-------------------------
ELEMENT1_SUFFIX1 2
ELEMENT1_SUFFIX2 4
ELEMENT2_SUFFIX1 5
ELEMENT2_SUFFIX2 15
ELEMENT1_RESULT 2
ELEMENT1_RESULT 3
可以使用以下代码生成该表。
with aux (elemento, valor) as
( select 'ELEMENT1_SUFFIX1', 2 from dual
UNION ALL
select 'ELEMENT1_SUFFIX2', 4 from dual
UNION ALL
select 'ELEMENT2_SUFFIX1', 5 from dual
UNION ALL
select 'ELEMENT2_SUFFIX2', 10 from dual
)
select aux.* from aux;
到目前为止,我一直在尝试这种方法,但我非常肯定一定有更好的解决方案。
with aux (elemento, valor) as
( select 'ELEMENT1_SUFFIX1', 2 from dual
UNION ALL
select 'ELEMENT1_SUFFIX2', 4 from dual
UNION ALL
select 'ELEMENT2_SUFFIX1', 5 from dual
UNION ALL
select 'ELEMENT2_SUFFIX2', 15 from dual
),
aux2 as
(select aux.*,
valor/max(
case
when regexp_substr(elemento, '[^_]+', 1, 2) = 'SUFFIX1'
then valor
end) over (partition by REGEXP_SUBSTR(elemento,'[^_]+',1,1)) new_value
from aux
)
select * from aux
union all
select REGEXP_SUBSTR(elemento,'[^_]+',1,1)
|| '_RESULT',
new_value valor
from aux2
where regexp_substr(elemento, '[^_]+', 1, 2) = 'SUFFIX2';
3条答案
按热度按时间xtfmy6hx1#
又一个变化。
样本数据:
质询:
1qczuiv02#
我们可以在这里使用聚合/透视方法沿着联合:
6fe3ivhb3#
试试这个: