oracle-如何“成对”地将行转换为列

hxzsmxv2  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(311)

有一个表基本上列出了字段名及其值,但每个属性和值都在一个单独的行中。在下例中,下表列出了产品代码及其价格。

create table attributes_list(attribute_name varchar2(50), attribute_value number);
insert into attributes_list values ('product_a_code', 10);
insert into attributes_list values ('product_b_code', 11);
insert into attributes_list values ('product_c_code', 12);
insert into attributes_list values ('product_a_price', 10.99);
insert into attributes_list values ('product_b_price', 20.99);
insert into attributes_list values ('product_c_price', 30.99);


是否可以将这些行转换为列,但我可以将价格链接到产品代码?

我知道题目不太理想,但我想不出更好的方法来回答这个问题。欢迎提出建议。

pbpqsu0x

pbpqsu0x1#

编写一个查询,{1}查找属性名称以''u code'结尾的所有行,{2}查找属性名称以''u price'结尾的所有行{3} 将产品名称上的两个结果集连接起来,可以通过substr()找到。

select 
  T1.attribute_value as product_code
, T2.attribute_value as product_price
from (                                    -- {1}
  select attribute_name, attribute_value 
  from attributes_list
  where attribute_name like '%_code'
) T1 join (                               -- {2}
  select attribute_name, attribute_value 
  from attributes_list
  where attribute_name like '%_price'
) T2
  -- {3} 
  on substr( T1.attribute_name, 1, length( T1.attribute_name ) - length( '_code' ) ) 
   = substr( T2.attribute_name, 1, length( T2.attribute_name ) - length( '_price' ) )
;

-- result
   PRODUCT_CODE    PRODUCT_PRICE 
_______________ ________________ 
             10            10.99 
             11            20.99 
             12            30.99

在这儿摆弄。
您可以使用regexp\u replace()—如@gmb的答案—在on子句中查找产品名称(而不是substr())。

qv7cva1a

qv7cva1a2#

您至少需要三列:

create table attributes_list (
    product varchar2(50),
    attribute_name varchar2(50),
    attribute_value number
);
insert into attributes_list values ('a', 'code', 10);
insert into attributes_list values ('b', 'code', 11);
. . .

然后可以使用条件聚合:

select max(case when attribute_name = 'code' then value end) as code,
       max(case when attribute_name = 'price' then value end) as price
from attributes_list
group by product;
wh6knrhe

wh6knrhe3#

如示例数据所示,如果 attribute_name 具有由产品和属性组成的固定格式(属性名称中没有下划线),可以使用正则表达式和条件聚合,如下所示:

select
    regexp_replace(attribute_name, '_[^_]+$', '') product_name,
    max(case when regexp_substr(attribute_name, '[^_]+$') = 'code'  then attribute_value end) product_code,
    max(case when regexp_substr(attribute_name, '[^_]+$') = 'price' then attribute_value end) product_price
from attributes_list 
group by regexp_replace(attribute_name, '_[^_]+$', '')

相关问题