如何将不同模式和不同数据类型的表的2个不同查询的组合结果合并到同一个表中

时间:2018-03-21 21:40:08

标签: sql geektool

我有2张桌子。 表A付款 表B电话

我想将差异模式的2个查询的结果合并到1个表中。

select Payment_DT  
from DW.Payment

SELECT PHONE_NUMBER 
FROM STG_ANALYSIS.PHONE

这是我要找的输出。

Payment_Dt  Phone_Number
3/31/2018   123-456-7890

2 个答案:

答案 0 :(得分:1)

不知道逻辑但要合并两者应该是

select Payment_DT,
      (SELECT PHONE_NUMBER 
      FROM STG_ANALYSIS.PHONE) phone_number  
from DW.Payment

如果存在连接两个表的逻辑,则使用内/左连接

答案 1 :(得分:0)

DB链接几乎就是这里游戏的名称。如果您无法自己创建一个,请检查是否有可以使用的公共数据库链接。

您的DBA也可能愿意使用其中一个DB链接在S1实例上创建S2.Table2的物化视图。

另一种选择可能是Web服务,但我猜你会遇到比使用简单数据库链接更多的管理问题。只有在没有链接的充分理由时才考虑这些(例如:两个不想在其数据库之间打开防火墙漏洞的独立组织)。

如果失败了,你会进入一个非常丑陋的领域,但你可能会有所作为。例如:

Open up both from a tool that can read from multiple connections at once and do the join there. Access. Toad for Data Analysis, whatever.
Use a tool like Toad to copy S2.Table2 to your own schema ("create in another schema" followed by "copy data to another schema")
If you have, or can get, complementary directory objects defined on both servers, create a Materialized View of S2 as an external table in a directory which can be written from S2 and read from S1.

但你确实不想长期维持这些解决方案。