H2,如何使用嵌套选择进行更新?

时间:2015-09-29 07:26:35

标签: sql database nested sql-update h2

我有两个表,EVENTEVENT_REV

EVENT:

╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║        1 ║ null               ║
║        2 ║ null               ║
║        3 ║ null               ║
║        4 ║ null               ║
║        5 ║ null               ║
╚══════════╩════════════════════╝

和EVENT_REV:

╔══════════════╦══════════╦═════════╦════════╦════════════╦══════════╗
║ EVENT_REV_ID ║ EVENT_ID ║ USER_ID ║ STATUS ║ VALID_FROM ║ VALID_TO ║
╠══════════════╬══════════╬═════════╬════════╬════════════╬══════════╣
║            1 ║        1 ║      54 ║      0 ║       1000 ║ 1001     ║
║            2 ║        1 ║      55 ║    100 ║       2000 ║ 2001     ║
║            3 ║        1 ║      56 ║    200 ║       3000 ║ 3001     ║
║            4 ║        2 ║      57 ║      0 ║       4000 ║ 4001     ║
║            5 ║        3 ║      58 ║      0 ║       5000 ║ 5001     ║
║            6 ║        3 ║      59 ║    100 ║       6000 ║ null     ║
║            7 ║        4 ║      60 ║      0 ║       7000 ║ null     ║
║            8 ║        5 ║      61 ║    500 ║       8000 ║ 8001     ║
║            9 ║        5 ║      62 ║    600 ║       9000 ║ 9001     ║
╚══════════════╩══════════╩═════════╩════════╩════════════╩══════════╝

我想更新EVENT表并设置SENT_INTO_WF_BY_ID 对此的规则是:

  • event_ids应匹配(EVENT.EVENT_ID = EVENT_REV.EVENT_ID)
  • 取STATUS不等于具有最低VALID_FROM的STATUS的行。哪个应该是第二个VALID_FROM
  • 的行
  • 从该行获取USER_ID

例如: 对于EVENT_ID = 1,它应该从EVENT_REV中选择第二行并将USER_ID 55放入SENT_INTO_WF_BY_ID

因为H2不允许内部联接,我的查询如下所示:

UPDATE event ltm
SET ltm.sent_into_wf_by_id =
  (SELECT top 1 ltmRev.user_id
   FROM event_rev ltmRev
   WHERE ltmRev.event_id = ltm.event_id
     AND ltmRev.status !=
       (SELECT top 1 EVENT_REV.status
        FROM EVENT_REV
        ORDER BY valid_from ASC nulls LAST)
   ORDER BY ltmRev.valid_to ASC nulls LAST)

结果如下:

╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║        1 ║ 55                 ║
║        2 ║ null               ║
║        3 ║ 59                 ║
║        4 ║ null               ║
║        5 ║ 62                 ║
╚══════════╩════════════════════╝

但实际上是:

╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║        1 ║ 55                 ║
║        2 ║ null               ║
║        3 ║ 59                 ║
║        4 ║ null               ║
║        5 ║ 61 <-- wrong       ║
╚══════════╩════════════════════╝

1 个答案:

答案 0 :(得分:1)

可以使用以下查询解决它:

UPDATE ltm_op_risk_event ltm
SET ltm.sent_into_wf_by_id =
  (SELECT ltmRev.adm_user_id
   FROM ltm_op_risk_event_rev ltmRev
   WHERE ltmRev.ltm_op_risk_event_id = ltm.ltm_op_risk_event_id
     AND ltmRev.status !=
       (SELECT ltmRev2.status
        FROM LTM_OP_RISK_EVENT_REV ltmRev2
        WHERE valid_from IS NOT NULL
          AND ltmRev.ltm_op_risk_event_id = ltmRev2.ltm_op_risk_event_id
        ORDER BY valid_from ASC LIMIT 1)
   ORDER BY ltmRev.valid_to ASC LIMIT 1)
WHERE ltm.sent_into_wf_by_id IS NULL;

缺少的部分是最里面的选择中的AND ltmRev.ltm_op_risk_event_id = ltmRev2.ltm_op_risk_event_id。我首先用错误的连接测试了这个连接......