【发布时间】:2019-12-02 05:55:59
【问题描述】:
我在 Oracle 中有以下实体-属性-值 (EAV) 表:
|身份证 |钥匙 |价值 | |----|-------------|--------------| | 1 |电话号码_1 | 111-111-1111 | | 1 | phone_num_2 | 222-222-2222 | | 1 |联系方式_1 |朋友 | | 1 |联系方式_2 |家庭 | | 1 |名字 |迈克 | | 1 |姓氏 |史密斯 | | 2 |电话号码_1 | 333-333-3333 | | 2 | phone_num_2 | 444-444-4444 | | 2 |联系方式_1 |家庭 | | 2 |联系方式_2 |朋友 | | 2 |名字 |约翰 | | 2 |姓氏 |亚当斯 | | 3 |电话号码_1 | 555-555-5555 | | 3 | phone_num_2 | 666-666-6666 | | 3 | phone_num_3 | 777-777-7777 | | 3 |联系方式_1 |工作 | | 3 |联系方式_2 |家庭 | | 3 |联系方式_3 |朋友 | | 3 |名字 |莫娜 | | 3 |姓氏 |丽莎 |请注意,某些键已编入索引,因此与其他已编入索引的键有关联。例如,phone_num_1 将与contact_1 关联。
注意:索引数量没有硬性限制。可以有10个、20个、甚至50个phone_num_*,但保证每个phone_num_N都有一个对应的contact_N
这是我想要的结果:
|身份证 |电话号码 |联系方式 |名字 |姓氏 | |----|-------------|---------|------------|------ -----| | 1 | 111-111-1111 |朋友 |迈克 |史密斯 | | 1 | 222-222-2222 |家庭 |迈克 |史密斯 | | 2 | 333-333-3333 |家庭 |约翰 |亚当斯 | | 2 | 444-444-4444 |朋友 |约翰 |亚当斯 | | 3 | 555-555-5555 |工作 |莫娜 |丽莎 | | 3 | 666-666-6666 |家庭 |莫娜 |丽莎 | | 3 | 777-777-7777 |朋友 |莫娜 |丽莎 |我尝试过/看过什么:
我研究了 Oracle 的枢轴功能;但是,我不相信这可以解决我的问题,因为我没有固定数量的属性,我想以此为中心。 我看过这些帖子: SQL Query to return multiple key value pairs from a single table in one row
Pivot rows to columns without aggregate
问题:
我想要完成的任务是否完全可以通过 SQL 完成?如果是这样,怎么办?如果不是,请解释原因。
非常感谢您提供任何帮助,这里是 with 表格,可帮助您入门:
with
table_1 ( id, key, value ) as (
select 1,'phone_num_1','111-111-1111' from dual union all
select 1,'phone_num_2','222-222-2222' from dual union all
select 1,'contact_1','friend' from dual union all
select 1,'contact_2','family' from dual union all
select 1,'first_name','mike' from dual union all
select 1,'last_name','smith' from dual union all
select 2,'phone_num_1','333-333-3333' from dual union all
select 2,'phone_num_2','444-444-4444' from dual union all
select 2,'contact_1','family' from dual union all
select 2,'contact_2','friend' from dual union all
select 2,'first_name','john' from dual union all
select 2,'last_name','adams' from dual union all
select 3,'phone_num_1','555-555-5555' from dual union all
select 3,'phone_num_2','666-666-6666' from dual union all
select 3,'phone_num_3','777-777-7777' from dual union all
select 3,'contact_1','work' from dual union all
select 3,'contact_2','family' from dual union all
select 3,'contact_3','friend' from dual union all
select 3,'first_name','mona' from dual union all
select 3,'last_name','lisa' from dual
)
select * from table_1;
【问题讨论】:
-
所谓的 EAV 模型被广泛质疑,因为查询起来既困难又慢(因此这个问题和许多人喜欢它)。不幸的是,由于那个神奇的词“灵活性”,它具有蟑螂般的生存能力。但是,这个问题提出了一个甚至不是 EAV 的模型。没有实体,只有具有公共前缀的键。伤心。
标签: sql oracle pivot entity-attribute-value