[英]SQL query to update a record based on the max value in another column
I have this table.我有这张桌子。 (You can also view it in DBFiddle .)
(您也可以在DBFiddle中查看它。)
Id ![]() |
Version![]() |
Item No.![]() |
Notes![]() |
---|---|---|---|
1 ![]() |
NULL ![]() |
31 ![]() |
|
2 ![]() |
1 ![]() |
31 ![]() |
tasty![]() |
3 ![]() |
2 ![]() |
31 ![]() |
kinda tasty![]() |
4 ![]() |
NULL ![]() |
32 ![]() |
|
5 ![]() |
1 ![]() |
32 ![]() |
meh![]() |
6 ![]() |
2 ![]() |
32 ![]() |
alright![]() |
7 ![]() |
3 ![]() |
32 ![]() |
fabulous![]() |
8 ![]() |
NULL ![]() |
33 ![]() |
ambivalent![]() |
9 ![]() |
1 ![]() |
33 ![]() |
gross![]() |
10 ![]() |
2 ![]() |
33 ![]() |
puke![]() |
The 1st column is the primary key.第一列是主键。 The 2nd and 3rd column are integers.
第二和第三列是整数。 The 4th column is a VARCHAR.
第 4 列是 VARCHAR。
For every unique Item No. where its Version is NULL, I want to look at the record with the highest Version value, take the content of its Notes field, and copy it over.对于版本为 NULL 的每个唯一项目编号,我想查看版本值最高的记录,将其注释字段的内容复制过来。
This is much easier to understand visually;这在视觉上更容易理解; after the command is run, the table should look like this:
命令运行后,表格应如下所示:
Id ![]() |
Version![]() |
Item No.![]() |
Notes![]() |
---|---|---|---|
1 ![]() |
NULL ![]() |
31 ![]() |
kinda tasty![]() |
2 ![]() |
1 ![]() |
31 ![]() |
tasty![]() |
3 ![]() |
2 ![]() |
31 ![]() |
kinda tasty![]() |
4 ![]() |
NULL ![]() |
32 ![]() |
fabulous![]() |
5 ![]() |
1 ![]() |
32 ![]() |
meh![]() |
6 ![]() |
2 ![]() |
32 ![]() |
alright![]() |
7 ![]() |
3 ![]() |
32 ![]() |
fabulous![]() |
8 ![]() |
NULL ![]() |
33 ![]() |
ambivalent![]() |
9 ![]() |
1 ![]() |
33 ![]() |
gross![]() |
10 ![]() |
2 ![]() |
33 ![]() |
puke![]() |
What is the query to achieve this in SQL Server?在 SQL 服务器中实现此目的的查询是什么?
I know that I need a way of grouping records together by Item No. , find the one with the highest Version value, take its Notes value, and copy it to the record where the Version is NULL, but I am having trouble translating this to SQL.我知道我需要一种按Item No.将记录分组的方法,找到版本值最高的那个,取其注释值,并将其复制到版本为 NULL 的记录中,但我无法将其转换为SQL。
Create a CTE to determine the value we need to update to, and then join that to your original table with update join syntax:创建一个 CTE 以确定我们需要更新的值,然后使用更新连接语法将其连接到原始表:
with maxVal as
(
select row_number() over (partition by [Item No.] order by Version desc) rn
, notes
, [Item No.]
from TestTable
where Version is not null
)
update TestTable
set Notes = maxVal.Notes
from TestTable
left join maxVal
on TestTable.[Item No.] = maxVal.[Item No.]
and maxVal.rn = 1
where TestTable.Version is null
and TestTable.Notes = ''
You can try this approach:您可以尝试这种方法:
-- Preparing a table variable
DECLARE @table1 AS TABLE (
id INT IDENTITY(1,1) NOT NULL,
VersionID INT,
ItemNo INT,
Note Varchar(50)
)
insert into
@table1 (VersionID,ItemNo,Note)
values
(NULL,31,''),
(1,31,'tasty'),
(2,31,'kinda tasty'),
(NULL,32,''),
(1,32,'meh'),
(2,32,'alright'),
(3,32,'fabulous'),
(NULL,33,'ambivalent'),
(1,33,'gross'),
(2,33,'puke');
-- update section
update up set up.Note = db.Note
from @table1 up
inner join
(
select a.ItemNo,a.Note
from @table1 a
inner join
(
select itemNo,Max(VersionID) as vs
from @table1
where Note <> ''
group by itemNo
) as b on a.ItemNo = b.ItemNo and a.VersionID = b.vs
) as db on db.ItemNo = up.ItemNo and up.Note = ''
-- result
select * from @table1
You can select the maximum value for each " Item No. ", then update the original table with the corresponding value where the " Notes " is NULL.您可以为每个“ Item No. ”选择最大值,然后用“ Notes ”为NULL的相应值更新原始表。
UPDATE tab
SET [Notes] = t2.[Notes]
FROM tab t1
INNER JOIN (SELECT *, MAX(Version) OVER(PARTITION BY [Item No.]) AS Max_Version
FROM tab) t2
ON t1.[Item No.] = t2.[Item No.]
AND t2.[Version] = t2.[Max_Version]
AND t1.[Notes] IS NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.