我想更新一个名为hours的列,其中每个作业记录的操作号都在39之前。每个作业由多个操作组成,最终操作始终为39号。例如,作业可能具有操作30,31,32,35和最终39,或者另一个作业可能具有30,31和39。在每种情况下,只有39之前需要更新的操作,在本例中为35和31。
我设法派生了一个Select脚本,列出了每个作业号的最大但只有一个操作号,但是无法派生更新版本来更新“小时”。对于每个匹配的记录。
SELECT jrt.job, max(jrt.oper_num) AS oper_num
FROM jrt_sch jrt
INNER JOIN job j ON j.job = jrt.job
WHERE j.type = 'S' AND jrt.job IN
(SELECT jrt.job
FROM jrt_sch jrt
INNER JOIN job j ON j.job = jrt.job
WHERE j.type = 'S'
GROUP BY jrt.job
HAVING max(jrt.oper_num) = 39)
AND oper_num < 39
GROUP BY jrt.job
这是
job oper_num
2109 31
2424 32
2509 32
2546 31
2418 31
3972 31
答案 0 :(得分:0)
将您的上述查询视为派生表,并在&#34; job&#34;并更新&#34;小时&#34;在oper_num =派生表中的oper_num的记录中。根据您的数据库产品,它应该如下所示:
UPDATE js
SET hours='some value'
FROM jrt_sch js
INNER JOIN (
<Your Query Above>
) m
ON js.job=m.job
AND js.oper_num=m.oper_num