【问题标题】:how to merge two tables to get the last new rows from table 2 and the rest from table 1如何合并两个表以从表 2 中获取最后的新行,从表 1 中获取其余行
【发布时间】:2020-06-09 03:20:08
【问题描述】:

我在将两个表连接到 oracle 数据库时遇到问题,但我不能这样做。 如果您在图像上看到,您可以有两个选择。

我执行这个查询:

SELECT f.id_hist, f.producto, f.price
FROM TABLE(fnc_historical('JAP')) f
    inner join new_table g on (f.id_new <> g.id_hist)
union
SELECT f.id_hist, f.producto, g.new_price
FROM TABLE(fnc_historical('JAP')) f
    inner join new_table g on (f.id_new = g.id_hist)

对于选项/案例 1,它有效

但对于选项/案例 2,此查询返回空,没有行。这个想法是必须从历史信息中返回所有行。

有人可以帮帮我吗?

非常感谢。

【问题讨论】:

    标签: sql oracle plsql oracle-apex


    【解决方案1】:

    只需使用NOT IN查找与new_table g不匹配的记录,并与INNER JOIN检索到的记录一起收集

    SELECT f.id_hist, f.producto, f.price
    FROM TABLE(fnc_historical('JAP')) f
    WHERE f.id_hist NOT IN
    (SELECT DISTINCT f.id_hist
    FROM TABLE(fnc_historical('JAP')) f
        inner join new_table g on (f.id_new = g.id_hist)
    )
    UNION
    SELECT f.id_hist, f.producto, g.new_price
    FROM TABLE(fnc_historical('JAP')) f
        inner join new_table g on (f.id_new = g.id_hist)
    

    【讨论】:

      【解决方案2】:

      我认为您正在寻找left join

      SELECT f.id_hist, f.producto,
             COALESCE(g.price, f.price) as price
      FROM TABLE(fnc_historical('JAP')) f LEFT JOIN
           new_table g 
           ON f.id_new = g.id_hist;
      

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        相关资源
        最近更新 更多