转置并插入具有不同值的多个行

时间:2013-03-20 20:30:34

标签: oracle plsql oracle11g pivot unpivot

我有一个更复杂的下表版本:

ID | FIRST  | LAST     | EMAIL
1  | John   | Doe      | jdoe@example.com
1  | Mack   | Johnson  | mjohnson@example.com
1  | Steven | Michaels | smichaels@example.com
2  | Sarah  | Sampson  | ssampson@example.com
2  | Tom    | Smith    | tsmith@example.com
2  | Jane   | Rogers   | jrogers@example.com
3  | Bob    | Johns    | bjohns@example.com
3  | Kim    | Lane     | klane@example.com
3  | Ron    | Swanson  | rswanson@example.com

我想编写一个查询,将此数据插入到另一个表中,看起来像这样(表已经存在):

ID | first1 | last1    | email1               | first2 | last2    | email2               | first3 | last3    | email3 
1  | John   | Doe      | jdoe@example.com     | Mack   | Johnson  | mjohnson@example.com | Steven | Michaels | smichaels@example.com
2  | Sarah  | Sampson  | ssampson@example.com | Tom    | Smith    | tsmith@example.com   | Jane   | Rogers      | jrogers@example.com
3  | Bob    | Johns    | bjohns@example.com   | Kim    | Lane     | klane@example.com    | Ron    | Swanson     | rswanson@example.com

我觉得这应该很容易,但这个概念让我望而却步。实现这一目标的最佳做法是什么?

也许我还应该提一下,我写了一个函数,我可以传递ID,索引号和列名来返回一个值(即getpersoninfo(2,' 1',&# 39;第一个')返回莎拉)。

select a_id,
  FIRST1, LAST1, EMAIL1,
  FIRST2, LAST2, EMAIL2,
  FIRST3, LAST3, EMAIL3
from
(
  select a_id, col||rn as new_col, value
  from
  (
    select a_id, first_name, last_name, email,
      cast(row_number() over(partition by a_id order by a_id) as varchar2(10)) rn
    from dump_recs_2015
  ) 
  unpivot
  (
    value
    for col in (first_name, last_name, email)
  )
) 
pivot
(
  max(value)
  for new_col in ('FIRST1' FIRST1, 'LAST1' LAST1, 'EMAIL1' EMAIL1,
                  'FIRST2' FIRST2, 'LAST2' LAST2, 'EMAIL2' EMAIL2,
                  'FIRST3' FIRST3, 'LAST3' LAST3, 'EMAIL3' EMAIL3)
);

1 个答案:

答案 0 :(得分:2)

由于您使用的是Oracle 11g,因此可以实现UNPIVOTPIVOT函数。

UNPIVOT会将您的列firstlastemail转换为行并将值转换为行。然后,您可以PIVOT将新名称First1等添加到列中:

select id,
  First1, Last1, Email1,
  First2, Last2, Email2,
  First3, Last3, Email3
from
(
  select id, col||rn as new_col, value
  from
  (
    select id, first, last, email,
      cast(row_number() over(partition by id order by id) as varchar2(10)) rn
    from yourtable
  ) 
  unpivot
  (
    value
    for col in (first, last, email)
  )
) 
pivot
(
  max(value)
  for new_col in ('FIRST1' First1, 'LAST1' Last1, 'EMAIL1' Email1,
                  'FIRST2' First2, 'LAST2' Last2, 'EMAIL2' Email2,
                  'FIRST3' First3, 'LAST3' Last3, 'EMAIL3' Email3)
) 

SQL Fiddle with Demo。然后可以在INSERT语句中使用它将数据加载到新表中。

编辑,根据您的列名更改,您将使用:

select a_id,
  First1, Last1, Email1,
  First2, Last2, Email2,
  First3, Last3, Email3
from
(
  select a_id, col||rn as new_col, value
  from
  (
    select a_id, first_name, last_name, email,
      cast(row_number() over(partition by a_id order by a_id) as varchar2(10)) rn
    from yourtable
  ) 
  unpivot
  (
    value
    for col in (first_name, last_name, email)
  )
) 
pivot
(
  max(value)
  for new_col in ('FIRST_NAME1' First1, 'LAST_NAME1' Last1, 'EMAIL1' Email1,
                  'FIRST_NAME2' First2, 'LAST_NAME2' Last2, 'EMAIL2' Email2,
                  'FIRST_NAME3' First3, 'LAST_NAME3' Last3, 'EMAIL3' Email3)
) 

请参阅SQL Fiddle with Demo