在 Oracle 中从多个表创建视图[重复]

问题描述 投票:0回答:1

在 Oracle 中从多个表创建视图时我完全迷失了。

这就是我所拥有的:

表1:
PKEY、姓名、街道

表2:
PKEY、REFPKEY、MUID、MUVAL

这些表格填写如下:
表1

1, Hans, 118th West
2, Dick, 1st Av.
3, etc ...

表2

1-1, 1, TEL, +1 56984
1-2, 1, EMA, [email protected]
2-1, 2, TEL, +49 7564862
2-2, 2, EMA, [email protected]
2-3, 2, MBT, +112911
3-1, etc ...

我想要的视图:

PKEY, NAME, Street,     Phone,        Email,            Mobile
1,    Hans, 118th West, +1 56984,    [email protected]
2,    Dick, 1st Av.,    +49 7564862, [email protected],  +112911
3, etc...

因此通过匹配table_1.pkey和table_2.refpkey来分配数据,视图的列名及其数据位于表2中。

我想它需要与 UNION 和 JOIN 周旋,但我几个小时都没有处理好。 有什么想法吗?

oracle view pivot
1个回答
0
投票

您需要条件聚合,如下所示:

Select t1.pkey, t1.name, t1.street,
       Max(case when t2.muid = 'TEL' then t2.muval end) as phone,
       Max(case when t2.muid = 'EMA' then t2.muval end) as email,
       Max(case when t2.muid = 'MBT' then t2.muval end) as mobile
  From table1 t1
  Left join table2 t2 on t1.pkey = t2.refpkey
Group by t1.pkey, t1.name, t1.street;
© www.soinside.com 2019 - 2024. All rights reserved.