使用此查询我需要SQL做的是根据另一列更新列值,以便它获取最新的MAX ID。
select
max(T1id), t1.v_code
from
[Table1] T1
join
[Table2] t2 on t1.T1Code = t2.T2Code
where
t2.active = 0
and t1.t1activesw = 0
and t1.mapping not like '%selected%'
group by
t1.v_code
我想在版本代码id = max(t1.v_code)上加入select到表格,然后将代码用作子选择而不确定如何完成它。
答案 0 :(得分:0)
通过这个,您可以从行中获取每个T1.v_code的最高T1.id的所有数据:
;WITH CTE as
(
SELECT
T1.*,
row_number() over (partition by T1.v_code order by T1.id desc) rn
FROM
[Table1] T1
JOIN
[Table2] t2 on t1.T1Code = t2.T2Code
WHERE
t2.active = 0
and t1.t1activesw = 0
and t1.mapping not like '%selected%'
)
SELECT *
FROM CTE
WHERE rn = 1
编辑:为了更新,替换(按照通知中的要求)
SELECT *
FROM CTE
WHERE rn = 1
带
UPDATE CTE
SET columnname = 'newvalue'
WHERE rn = 1
此