具有多个 Unpivot 或 For 的列

Columns With Multiple Unpivot or For

我有下面的table结构

CREATE TABLE test_unpivot
(
   prod_id              NUMBER,
   prod_name_reuse      VARCHAR2 (1),
   prod_cat_reuse       VARCHAR2 (1),
   prod_name_transfer   VARCHAR2 (1),
   prod_cat_transfer    VARCHAR2 (1),
   remarks              VARCHAR2 (10)
);

我正在使用 unpivot 作为

    SELECT prod_id, value1, col1
  FROM test_unpivot2 UNPIVOT (value1
                     FOR col1
                     IN (prod_name_reuse, prod_cat_reuse))
 WHERE prod_id = 120

并给出结果

+---------+--------+-----------------+
| PROD_ID | VALUE1 |      COL1       |
+---------+--------+-----------------+
|     120 | Y      | PROD_NAME_REUSE |
|     120 | N      | PROD_CAT_REUSE  |
+---------+--------+-----------------+

我的问题是使用UNPIVOT,是否可以达到以下结果? 即 prod_name_transfer 是另一列,应显示为 value2。 UNPIVOT 是否支持多个 FOR..IN 语句?

+---------+--------+-----------------+--------+---------------------+
| PROD_ID | VALUE1 |      COL1       |VALUE2 |      COL2            |
+---------+--------+-----------------+--------+---------------------+
|     120 | Y      | PROD_NAME_REUSE | Y      | PROD_NAME_TRANSFER  |
|     120 | N      | PROD_CAT_REUSE  | N      | PROD_NAME_TRANSFER  |
+---------+--------+-----------------+------------------------------+

Test table and data

我的实际 table 有大约 54 列,其中有很多记录。

Table结构

CREATE TABLE test_unpivot2
(
   prod_id              NUMBER,
   prod_name_reuse      VARCHAR2 (1),
   prod_cat_reuse       VARCHAR2 (1),
   prod_name_transfer   VARCHAR2 (1),
   prod_cat_transfer    VARCHAR2 (1),
   remarks              VARCHAR2 (10)
);

Table数据

INSERT INTO test_unpivot2
     VALUES (120,
             'Y',
             'N',
             'Y',
             'N',
             'Test1');

INSERT INTO test_unpivot2
     VALUES (121,
             'Y',
             'N',
             'Y',
             'N',
             'Test2');

INSERT INTO test_unpivot2
     VALUES (122,
             'Y',
             'N',
             'Y',
             'N',
             'Test3');

INSERT INTO test_unpivot2
     VALUES (123,
             'Y',
             'N',
             'Y',
             'N',
             'Test4');

COMMIT

你甚至需要使用 UNPIVOT 吗?

SELECT id,
       prod_name_reuse      AS value1,
       'PROD_NAME_REUSE'    AS col1,
       prod_name_transfer   AS value2,
       'PROD_NAME_TRANSFER' AS col2
FROM   test_unpivot
WHERE  id = 120
UNION ALL
SELECT id,
       prod_cat_reuse      AS value1,
       'PROD_CAT_REUSE'    AS col1,
       prod_cat_transfer   AS value2,
       'PROD_CAT_TRANSFER' AS col2
FROM   test_unpivot
WHERE  id = 120;

更新:

SELECT *
FROM   (
  SELECT u.*,
         REGEXP_SUBSTR( type, '_(.+?)_', 1, 1, NULL, 1 ) AS namecat,
         REGEXP_SUBSTR( type, '[^_]+$' ) AS reusetransfer
  FROM   testunpivot
  UNPIVOT (
    value
    FOR type IN (
      prod_name_reuse,
      prod_cat_reuse,
      prod_name_transfer,
      prod_cat_transfer
    )
  ) u
)
PIVOT (
  MAX( value ) AS value,
  MAX( type  ) AS col
  FOR reusetransfer IN ( 'REUSE', 'TRANSFER' )
);