Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 将行动态转换为列_Oracle_Plsql - Fatal编程技术网

Oracle 将行动态转换为列

Oracle 将行动态转换为列,oracle,plsql,Oracle,Plsql,我有这个表,条件是一个产品最多可以有4个标签4个标签名称/LN,4个标签放置/LP: PRODUCT_CODE LABEL_NAME PLACEMENT_DETAIL --------------------------------------------- 307960-010 Trademark Bottom Left 307960-010 228119 Middle Left 307960-010 YCM Sticker Bottom R

我有这个表,条件是一个产品最多可以有4个标签4个标签名称/LN,4个标签放置/LP:

PRODUCT_CODE    LABEL_NAME  PLACEMENT_DETAIL
---------------------------------------------
307960-010      Trademark   Bottom Left
307960-010      228119      Middle Left
307960-010      YCM Sticker Bottom Right
307960-015      Trademark   Bottom Left
307960-016      Trademark   Bottom Left
307960-017      Trademark   Bottom Left
307960-020      228119      Middle Left
307960-020      Trademark   Bottom Left
我想显示如下表:

Product Code LN1 LP1 LN2 LP2 LN3 LP3 LN4 LP4
307960-010 Trademark Bottom Left 228119 Middle Left YCM Sticker Bottom Right 307960-015 Trademark Bottom Left 307960-016 Trademark Bottom Left 307960-017 Trademark Bottom Left 307960-020 228119 Middle Left Trademark Bottom Left 结果如下:

PRODUCT_CODE LN1 LP1 LN2 LP2 LN3 LP3
307960-010 Trademark Bottom Left 307960-010 228119 Middle Left 307960-010 YCM Sticker Bottom Right 307960-015 Trademark Bottom Left 307960-016 Trademark Bottom Left 307960-017 Trademark Bottom Left 307960-020 228119 Middle Left 307960-020 Trademark Bottom Left 如何达到这样的效果

谢谢。

你想吗

在执行此操作之前,请按产品代码为每个产品代码分区分配一个从一开始的行号,并根据您希望对标签进行优先级排序的方式进行排序

在pivot子句中,针对产品的每个可能条目数,取标签和位置的最小值或最大值:

create table t (
  product_code varchar2(10),
  label_name   varchar2(20),
  placement    varchar2(20)
);

insert into t values ( '307960-010', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-010', '228119', 'Middle Left' );
insert into t values ( '307960-010', 'YCM Sticker', 'Bottom Right' );
insert into t values ( '307960-015', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-016', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-017', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-020', '228119', 'Middle Left' );
insert into t values ( '307960-020', 'Trademark', 'Bottom Left' );

with rws as (
  select t.*, 
         row_number () over (
           partition by product_code
           order by label_name, placement
         ) rn
  from   t
)
  select * from rws
  pivot (
    min ( label_name ) ln, min ( placement ) lp
    for rn in ( 1, 2, 3 )
  );
  
PRODUCT_CODE    1_LN         1_LP           2_LN         2_LP           3_LN           3_LP           
307960-010      228119       Middle Left    Trademark    Bottom Left    YCM Sticker    Bottom Right    
307960-015      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-016      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-017      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-020      228119       Middle Left    Trademark    Bottom Left    <null>         <null> 

-25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用是不鼓励的。谢谢,我会记住避免旧样式的连接。太好了,现在它显示了我想要的。谢谢你,先生,我需要了解更多。祝你今天愉快。
create table t (
  product_code varchar2(10),
  label_name   varchar2(20),
  placement    varchar2(20)
);

insert into t values ( '307960-010', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-010', '228119', 'Middle Left' );
insert into t values ( '307960-010', 'YCM Sticker', 'Bottom Right' );
insert into t values ( '307960-015', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-016', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-017', 'Trademark', 'Bottom Left' );
insert into t values ( '307960-020', '228119', 'Middle Left' );
insert into t values ( '307960-020', 'Trademark', 'Bottom Left' );

with rws as (
  select t.*, 
         row_number () over (
           partition by product_code
           order by label_name, placement
         ) rn
  from   t
)
  select * from rws
  pivot (
    min ( label_name ) ln, min ( placement ) lp
    for rn in ( 1, 2, 3 )
  );
  
PRODUCT_CODE    1_LN         1_LP           2_LN         2_LP           3_LN           3_LP           
307960-010      228119       Middle Left    Trademark    Bottom Left    YCM Sticker    Bottom Right    
307960-015      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-016      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-017      Trademark    Bottom Left    <null>       <null>         <null>         <null>          
307960-020      228119       Middle Left    Trademark    Bottom Left    <null>         <null>