Oracle 12にテーブルがあり、edit_numberのすべての行が複数の列を持つ1つの行にロールアップされるように、テーブルをフラット化する必要があります。これは以前に見たことがありますが、特定の列に配置する方法を見つけるのに苦労しています。Edit_NumberごとにEdit_Raw_Tableに最大4行あります
Edit_Raw_Table
+----------+-------------+---------+------------+------------+
| 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>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加