简体   繁体   English

根据联接表的日期列之一的最大日期值执行更新(Oracle)

[英]Perform Update Based On Max Date Value Of One of the Join Table's Date Column (Oracle)

I was trying to update the "description" column of Table_H according to its complex relationship with other tables. 我试图根据其与其他表的复杂关系来更新Table_H的“描述”列。 Here is the original (somewhat obfiscated) query: 这是原始查询(有些被混淆):

        UPDATE Table_H hb SET 
        description = 'New Description'
        where exists
        (select 1
        from TABLE_J j, TABLE_I i, TABLE_K k, Table_H h, TABLE_M m
        where j.Alt_id = i.Alt_id
        and i.id=k.id
        and k.id=h.id
        and k.Alt_id = m.Alt_id
        and j.Activity='Activity A'
        and h.description = 'Old Description'
        and h.id=hb.id
        );

The problem with this query was that it updated too many Table_H rows (as those rows related to the rows of the other tables in the query). 此查询的问题在于它更新了太多的Table_H行(因为这些行与查询中其他表的行相关)。 This was because Table_H had multiple rows that were identical with the exception of the Table_H.date field, which was always unique. 这是因为Table_H具有多个相同的行,但Table_H.date字段除外,该行始终是唯一的。 What I actually needed to do was update only the Table_H rows with the MAX Table_H.date field. 我实际上需要做的是仅使用MAX Table_H.date字段更新Table_H行。

I have not been able to find an old thread that is particularly close to my situation. 我找不到适合我情况的旧线程。 Obviously I have to introduce the Table_H.Date column but not sure how to proceed. 显然,我必须介绍Table_H.Date列,但不确定如何进行。 I would be grateful for any response. 我将不胜感激。 Thank you 谢谢

I hope you need something like this. 我希望你需要这样的东西。

UPDATE Table_H hb
SET description = 'New Description'
WHERE EXISTS
  (SELECT 1
  FROM TABLE_J j,
    TABLE_I i,
    TABLE_K k,
    Table_H h,
    TABLE_M m
  WHERE j.Alt_id    = i.Alt_id
  AND i.id          =k.id
  AND k.id          =h.id
  AND k.Alt_id      = m.Alt_id
  AND j.Activity    ='Activity A'
  AND h.description = 'Old Description'
  AND h.id          =hb.id
  )
AND hb.date_t IN
  ( SELECT MAX(Date_t) FROM Table_H
  );

Here Date_t is your date column 这里Date_t是您的date

I appreciate the responses. 感谢您的回应。 I found out a way to do this without directly using the date column. 我找到了一种无需直接使用日期列即可执行此操作的方法。 I used the max(table_h.id) column instead (I realized the max id corresponded directly to the max(table_h.date) and this made the query earlier to write). 我改用了max(table_h.id)列(我意识到max id直接对应于max(table_h.date),这使得查询更早了。 Here is the solution. 这是解决方案。 I apologize if this is not a very good learning opportunity for others in stackoverflow: 对于这不是Stackoverflow中其他人的很好学习机会,我深表歉意:

UPDATE Table_H hb
SET description = 'New Description'
WHERE EXISTS
  (SELECT 1 from
(select max(h.id) hid, k.Alt_id
FROM TABLE_J j,
TABLE_I i,
TABLE_K k,
Table_H h,
TABLE_M m
WHERE j.Alt_id    = i.Alt_id
AND i.id          =k.id
AND k.id          =h.id
AND k.Alt_id      = m.Alt_id
AND j.Activity    ='Activity A'
AND h.description = 'Old Description'
GROUP BY k.alt_id) abc
WHERE abc.hid = hb.id
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM