Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
如何在pl sql中透视列_Sql_Oracle - Fatal编程技术网

如何在pl sql中透视列

如何在pl sql中透视列,sql,oracle,Sql,Oracle,我有一个相当复杂的遗留sql查询,它为这样的文档选择属性列表: 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

我有一个相当复杂的遗留sql查询,它为这样的文档选择属性列表:

 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
我害怕更改遗留sql中的任何内容,因此我希望将现有输出转换为如下内容:

 type |  name  | attr-A | attr-B
----------------------------------
  1   | doc-1  |  1003  | string1
  2   | doc-2  |  1004  | string2
  1   | doc-3  |  1005  | string3
  • 对于类型为1的每个文档:attr-A是attr=10的属性(使用 int_val),attr-B是attr=15的属性(使用str_val)
  • 对于类型为2的每个文档:attr-A是attr=13的属性(使用int\u val),attr-B是attr=22的属性(使用str\u val)

请帮助我以我建议的方式编写处理现有输出的查询

您可以根据和案例对逻辑进行分组:

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;

输出:

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

试试这个。它将帮助你解决你的问题。如果有任何问题,请告诉我

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----------------------------------

谢谢,我很快会查的!