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
Sql 试图在oracle中将数据展平为列_Sql_Oracle_Pivot - Fatal编程技术网

Sql 试图在oracle中将数据展平为列

Sql 试图在oracle中将数据展平为列,sql,oracle,pivot,Sql,Oracle,Pivot,我在Oracle12中有一个表,我需要展平该表,以便将所有用于编辑编号的行汇总成一行多列。我以前见过这种情况,但我正在努力找到一种方法将它们放在特定的专栏中。每个编辑\u编号的编辑\u原始\u表中最多有4行 编辑原始表格 +----------+-------------+---------+------------+------------+ | table_ID | Edit_Number | Edit_Ref| Edit_Text | Edit_Valid | +----------+-

我在Oracle12中有一个表,我需要展平该表,以便将所有用于编辑编号的行汇总成一行多列。我以前见过这种情况,但我正在努力找到一种方法将它们放在特定的专栏中。每个编辑\u编号的编辑\u原始\u表中最多有4行

编辑原始表格

+----------+-------------+---------+------------+------------+
| table_ID | Edit_Number | Edit_Ref| Edit_Text  | Edit_Valid |
+----------+-------------+---------+------------+------------+
|        1 |        3    |   10146 | REASON 123 |   YES      |
|        2 |        3    |   10169 | REASON 567 |   YES      |
|        3 |        3    |   10156 | REASON 456 |    NO      |
+----------+-------------+---------+------------+------------+
我想做的是这样的:

+------------+-----------+------------+------------+-------------+
| Edit_Number| Edit_Ref_1| Edit_Text_1| Edit_Ref_2 | Edit_Text_2 | 
+------------+-----------+------------+------------+-------------+
|       3    |   10146   | Reason 123 |    10169   |  Reason 567 |
+------------+-----------+------------+------------+-------------+

一点聚合加上
decode
(或
case
,随便你喜欢;在这种情况下,
decode
非常简单)就可以完成这项工作

SQL> with edit_raw_table (table_id, edit_number, edit_ref, edit_text, edit_valid) as
  2    (select 1, 3, 10146, 'Reason 123', 'yes' from dual union all
  3     select 2, 3, 10169, 'Reason 567', 'yes' from dual union all
  4     select 3, 3, 10156, 'Reason 456', 'no'  from dual union all
  5     --
  6     select 4, 8, 10111, 'reason 111', 'yes' from dual union all
  7     --
  8     select 5, 4, 20222, 'reason 222', 'no'  from dual union all
  9     select 6, 4, 20333, 'reason 333', 'yes' from dual union all
 10     select 7, 4, 20444, 'reason 444', 'yes' from dual union all
 11     select 8, 4, 20555, 'reason 555', 'yes' from dual
 12    ),
 13  temp as
 14    (select e.*,
 15            row_number() over (partition by edit_number order by table_id) rn
 16     from edit_raw_table e
 17     where edit_valid = 'yes'
 18    )
 19  select edit_number,
 20         max(decode(rn, 1, edit_ref)) edit_ref_1,
 21         max(decode(rn, 1, edit_text)) edit_text_1,
 22         --
 23         max(decode(rn, 2, edit_ref)) edit_ref_2,
 24         max(decode(rn, 2, edit_Text)) edit_text_2,
 25         --
 26         max(decode(rn, 3, edit_ref)) edit_ref_3,
 27         max(decode(rn, 3, edit_Text)) edit_text_3,
 28         --
 29         max(decode(rn, 4, edit_ref)) edit_ref_4,
 30         max(decode(rn, 4, edit_Text)) edit_text_4
 31  from temp
 32  group by edit_number
 33  order by edit_number;

EDIT_NUMBER EDIT_REF_1 EDIT_TEXT_ EDIT_REF_2 EDIT_TEXT_ EDIT_REF_3 EDIT_TEXT_ EDIT_REF_4 EDIT_TEXT_
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
          3      10146 Reason 123      10169 Reason 567
          4      20333 reason 333      20444 reason 444      20555 reason 555
          8      10111 reason 111

SQL>

对,看起来它可以在t-sql中工作,但不能在PL/sql中工作。我不确定是否应该包含我尝试过的内容。我删除了代码,因为它与最终目标无关。这是可行的,当然可以用当前表中的select替换所选值。非常感谢。