如何编写一个 SQL 查询,将 phone 数字从列转换为单个列?
How do write an SQL query that transforms phone numbers from columns into a single column?
如何编写一个 SQL 查询,将 phone 个数字从列转换为单个列。假设有多个 personID,每个 personID 最多有 3 phone 种类型,Primary、Secondary 和 Tertiary。目前,对于每个人,他们都列在三个单独的列中。
期望的结果是 phone 个数字都在一列中,而另一列有 phone 个类型
当前数据
Person_ID
Primary_Phone
Secondary_Phone
Tertiary_Phone
1
2221111111
5551111111
9991111111
2
2221111112
5551111112
9991111112
3
2221111113
5551111113
9991111113
4
2221111114
5551111114
9991111114
所需数据
Person_ID
Phone_Number
Phone_Type
1
2221111111
Primary
1
5551111111
Secondary
1
9991111111
Tertiary
2
2221111112
Primary
2
5551111112
Secondary
2
9991111112
Tertiary
3
2221111113
Primary
3
5551111113
Secondary
3
9991111113
Tertiary
4
2221111114
Primary
4
5551111114
Secondary
4
9991111114
Tertiary
在 Oracle 起始版本 12c 中,您可以使用 cross apply
:
将列反透视为行
select t.person_id, x.*
from mytable t
cross apply (
select primary_phone as phone_number, 'Primary' as phone_type from dual
union all select secondary_phone, 'Secondary' from dual
union all select tertiary_phone, 'Tiertiary' from dual
) x
在早期版本中,您可以使用 union all
:
select person_id, primary_phone as phone_number, 'Primary' as phone_type from mytable
union all select person_id, secondary_phone, 'Secondary' from mytable
union all select person_id, tertiary_phone, 'Tiertiary' from mytable
您似乎想做一个 unpivot
with p as (
select 1 person_id,
'2221111111' primary_phone,
'5551111111' secondary_phone,
'9991111111' tertiary_phone
from dual
union all
select 2,
'2221111112' primary_phone,
'5551111112' secondary_phone,
'9991111112' tertiary_phone
from dual
)
select person_id,
phone_number,
phone_type
from p
unpivot (
phone_number
for phone_type in (
primary_phone as 'Primary',
secondary_phone as 'Secondary',
tertiary_phone as 'Tertiary'
)
)
A liveSQL link 显示查询 运行
如何编写一个 SQL 查询,将 phone 个数字从列转换为单个列。假设有多个 personID,每个 personID 最多有 3 phone 种类型,Primary、Secondary 和 Tertiary。目前,对于每个人,他们都列在三个单独的列中。
期望的结果是 phone 个数字都在一列中,而另一列有 phone 个类型
当前数据
Person_ID | Primary_Phone | Secondary_Phone | Tertiary_Phone |
---|---|---|---|
1 | 2221111111 | 5551111111 | 9991111111 |
2 | 2221111112 | 5551111112 | 9991111112 |
3 | 2221111113 | 5551111113 | 9991111113 |
4 | 2221111114 | 5551111114 | 9991111114 |
所需数据
Person_ID | Phone_Number | Phone_Type |
---|---|---|
1 | 2221111111 | Primary |
1 | 5551111111 | Secondary |
1 | 9991111111 | Tertiary |
2 | 2221111112 | Primary |
2 | 5551111112 | Secondary |
2 | 9991111112 | Tertiary |
3 | 2221111113 | Primary |
3 | 5551111113 | Secondary |
3 | 9991111113 | Tertiary |
4 | 2221111114 | Primary |
4 | 5551111114 | Secondary |
4 | 9991111114 | Tertiary |
在 Oracle 起始版本 12c 中,您可以使用 cross apply
:
select t.person_id, x.*
from mytable t
cross apply (
select primary_phone as phone_number, 'Primary' as phone_type from dual
union all select secondary_phone, 'Secondary' from dual
union all select tertiary_phone, 'Tiertiary' from dual
) x
在早期版本中,您可以使用 union all
:
select person_id, primary_phone as phone_number, 'Primary' as phone_type from mytable
union all select person_id, secondary_phone, 'Secondary' from mytable
union all select person_id, tertiary_phone, 'Tiertiary' from mytable
您似乎想做一个 unpivot
with p as (
select 1 person_id,
'2221111111' primary_phone,
'5551111111' secondary_phone,
'9991111111' tertiary_phone
from dual
union all
select 2,
'2221111112' primary_phone,
'5551111112' secondary_phone,
'9991111112' tertiary_phone
from dual
)
select person_id,
phone_number,
phone_type
from p
unpivot (
phone_number
for phone_type in (
primary_phone as 'Primary',
secondary_phone as 'Secondary',
tertiary_phone as 'Tertiary'
)
)
A liveSQL link 显示查询 运行