简体   繁体   中英

How do I pivot columns in pl-sql

I have rather complicated legacy sql query that selects attributes list for documents like that:

 type |  name   | attr | int_val |  str_val 
--------------------------------------------
  1   |  doc-1  |  10  |  1003   |   null
  1   |  doc-1  |  15  |  null   |  string1
  2   |  doc-2  |  13  |  1004   |   null
  2   |  doc-2  |  22  |  null   |  string2
  1   |  doc-3  |  10  |  1005   |   null
  1   |  doc-3  |  15  |  null   |  string3

I'm afraid to change anything inside legacy sql so I'd like to convert exising ouput to something like:

 type |  name  | attr-A | attr-B
----------------------------------
  1   | doc-1  |  1003  | string1
  2   | doc-2  |  1004  | string2
  1   | doc-3  |  1005  | string3
  • For every document with type=1: attr-A is attribute with attr=10 (use int_val), attr-B is attribute with attr=15 (use str_val)
  • For every document with type=2: attr-A is attribute with attr=13 (use int_val), attr-B is attribute with attr=22 (use str_val)

Please help me to write the query that processes the existing output the way I proposed.

You can GROUP BY and CASE WHEN for your logic:

SELECT type, name,
    MAX(CASE WHEN type = 1 AND attr = 10 THEN int_val 
             WHEN type = 2 AND attr = 13 THEN int_val
       END) AS AttrA,
    MAX(CASE WHEN type = 1 AND attr = 15 THEN str_val 
             WHEN type = 2 AND attr = 22 THEN str_val 
       END) AS AttrB
FROM your_table
GROUP BY type, name;

LiveDemo

Output:

╔══════╦═══════╦═══════╦═════════╗
║ type ║ name  ║ attrA ║  attrB  ║
╠══════╬═══════╬═══════╬═════════╣
║    1 ║ doc-1 ║  1003 ║ string1 ║
║    2 ║ doc-2 ║  1004 ║ string2 ║
║    1 ║ doc-3 ║  1005 ║ string3 ║
╚══════╩═══════╩═══════╩═════════╝

Try this. It will help you to resolve your problem. Let me know for any issues.

SELECT a.typ,
  a.nme,
  MAX(a.int_val),
  MAX(a.str_val)
FROM
  (SELECT 1 AS typ,'doc-1' nme,10 attr,1003 int_val,NULL AS str_val FROM dual
  UNION ALL
  SELECT 1 AS typ,
    'doc-1' nme,
    15 attr,
    NULL int_val,
    'string1' AS str_val
  FROM dual
  UNION ALL
  SELECT 2 AS typ,'doc-2' nme,13 attr,1004 int_val,NULL AS str_val FROM dual
  UNION ALL
  SELECT 2 AS typ,
    'doc-2' nme,
    22 attr,
    NULL int_val,
    'string2' AS str_val
  FROM dual
  UNION ALL
  SELECT 1 AS typ,'doc-3' nme,10 attr,1005 int_val,NULL AS str_val FROM dual
  UNION ALL
  SELECT 1 AS typ,
    'doc-3' nme,
    15 attr,
    NULL int_val,
    'string3' AS str_val
  FROM dual
  )a
GROUP BY a.typ,
  a.nme;

------------------------------OUTPUT----------------------------------
TYP NME MAX(A.INT_VAL)  MAX(A.STR_VAL)
1   doc-3   1005    string3
2   doc-2   1004    string2
1   doc-1   1003    string1
------------------------------OUTPUT----------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM