【问题标题】:Oracle Update from Select - same table来自 Select 的 Oracle 更新 - 同一张表
【发布时间】:2018-03-04 23:21:10
【问题描述】:

我正在尝试使用 date2、date3、date4 列中的最高日期来更新 date1 列。 我有选择语句

SELECT GREATEST(
    COALESCE(date2, date3, date4),
    COALESCE(date3, date4, date2),
    COALESCE(date4, date2, date3)
)
FROM (
    SELECT date1,date2,date3,date4 
    FROM my_table
    WHERE date1 is not null and (date1 < date2 or date1 < date3 or date1 < date4);

但我不知道如何将它与更新或合并一起使用??

+------------------+------------------+------------------+------------------+ | date1 | date2 | date3 | date4 | +------------------+------------------+------------------+------------------+ | 2017-04-13 16:54 | 2017-04-13 16:57 | | 2016-06-16 | | 2017-04-13 15:41 | | 2017-04-13 15:42 | 2016-06-16 | | 2017-04-13 15:43 | 2018-01-10 14:23 | 2017-04-13 15:41 | | | 2017-04-13 16:05 | | 2017-04-13 16:05 | 2016-06-16 | | 2017-04-13 16:43 | 2017-04-13 16:43 | | 2016-06-16 | | 2017-04-13 16:52 | 2017-04-13 16:52 | 2017-04-13 16:07 | 2017-04-17 16:07 | | 2018-01-10 14:20 | | 2018-01-10 14:23 | | | 2017-09-27 14:54 | 2015-09-08 09:56 | 2017-09-27 14:54 | 2015-03-13 | | 2017-06-16 13:38 | | 2017-06-16 13:39 | | +------------------+------------------+------------------+------------------+

更新/合并后我想要类似的东西

+------------------+------------------+------------------+------------------+ | date1 | date2 | date3 | date4 | +------------------+------------------+------------------+------------------+ | 2017-04-13 16:57 | 2017-04-13 16:57 | | 2016-06-16 | | 2017-04-13 15:42 | | 2017-04-13 15:42 | 2016-06-16 | | 2018-01-10 14:23 | 2018-01-10 14:23 | 2017-04-13 15:41 | | | 2017-04-13 16:05 | | 2017-04-13 16:05 | 2016-06-16 | | 2017-04-13 16:43 | 2017-04-13 16:43 | | 2016-06-16 | | 2017-04-17 16:07 | 2017-04-13 16:52 | 2017-04-13 16:07 | 2017-04-17 16:07 | | 2018-01-10 14:23 | | 2018-01-10 14:23 | | | 2017-09-27 14:54 | 2015-09-08 09:56 | 2017-09-27 14:54 | 2015-03-13 | | 2017-06-16 13:39 | | 2017-06-16 13:39 | | +------------------+------------------+------------------+------------------+

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    你应该可以做到:

    update t
        set date1 = greatest(date1, coalesce(date2, date1), coalesce(date3, date1), coalesce(date4, date1))
        where date1 < greatest(date1, coalesce(date2, date1), coalesce(date3, date1), coalesce(date4, date1));
    

    在您的数据中,date1 似乎永远不会是 NULL

    【讨论】:

    • OP 在他/她的代码中有where date1 is not null,所以大概date1 可以是null(如果不是今天的话,在未来)。但是在这种情况下,您的语句会将其更新为null,这很好(无论如何都不应该更新),除非它会更新一行,这通常不是一个有效的选择。您可以通过添加where 子句来改进此解决方案(这非常好!),这将使update 尽可能高效。添加:where date1 &lt; greatest(date1, coalesce(date2, date1), ...) - 与set 子句中的公式相同。 +1(如果可能的话,还有更多)
    • @mathguy 。 . .鉴于 OP 有这个条件,它也应该在答案中。
    • 对。我的建议(与where date1 is not null 不同)将保证date1 is not null,但它也会阻止date1 被更新到自身 - 如果更新应该(否则)只影响相对较少的行数尤其重要. OP 已经有了完整的where 子句,而不仅仅是date1 is not null
    猜你喜欢
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    相关资源
    最近更新 更多