0

我需要使用下一条记录的数据(根据增量 gkey 字段)更新日期错误(1970-01-01)的表的记录。

所以,如果我做这个查询:

SELECT aa.gkey,
       aa.course_date,
       (select course_date from BI.fact_training_event_tbl bb where bb.gkey = (
            select min(cc.gkey) 
            from BI.fact_training_event_tbl cc 
                 where cc.gkey > aa.gkey)) as next_date
from BI.fact_training_event_tbl aa
where course_date = '1970-01-01'

正如预期的那样,它正确地带来了记录:

gkey   course_date  next_date
====   ===========  =========
4103   1970-01-01   2017-03-23
4884   1970-01-01   2017-03-22
5047   1970-01-01   2017-03-23

我现在需要用 next_date 更新 course_date 字段,但是如果我尝试运行以下命令,我会得到

错误代码 1093。您不能在 FROM 子句中指定目标表 'aa' 进行更新:

update BI.fact_training_event_tbl aa
    set course_date =
    (select course_date from BI.fact_training_event_tbl bb where bb.gkey = (
            select min(cc.gkey)
    from BI.fact_training_event_tbl cc
         where cc.gkey > aa.gkey))
where course_date = '1970-01-01'

关于如何解决这个问题的任何想法?

我想澄清一下,这个问题之前没有得到回答,因为关于错误 1093 的线程显示了一个简单的子查询。就我而言,我正在查找引用主表的下一条记录。请不要将其标记为重复。

4

1 回答 1

0

您可以尝试使用表名而不是别名,因为内部子查询不知道别名aa

update BI.fact_training_event_tbl
set course_date =
    (
    select course_date 
    from BI.fact_training_event_tbl bb 
    where bb.gkey = 
        ( 
            select min(cc.gkey)
            from BI.fact_training_event_tbl cc
            where cc.gkey > BI.fact_training_event_tbl.gkey
        )
    )
where course_date = '1970-01-01'
于 2018-10-26T21:36:22.820 回答