Answer the question
In order to leave comments, you need to log in
T-SQL. How to UPDATE records in a table that have the same, for example, GroupID, but different TypeID?
How to make a query so that we can update the records (1, 3, N'1 Dmitry', 0) and (2, 3, N'2 Sergey', 0) by GroupID, we define two records, one of which will receive the value Number from another. If the entry has TypeID = 1, then you need to take a value from this entry and assign it to the entry that has the same GroupID and TypeID = 3.
That is, in the end it will be
(1, 1, N'1 Vladimir', 10 ),
(1 , 3, N'1 Dmitry', 10 ), and
(2, 1, N'2 Andrey', 20 ),
(2, 3, N'2 Sergey', 20 ).
IF OBJECT_ID('Users') IS NOT NULL
DROP TABLE Users;
CREATE TABLE Users(
GroupID INT NOT NULL,
TypeID INT NOT NULL,
Name NVARCHAR(40) NOT NULL,
Number INT NOT NULL,
CONSTRAINT PK_Users_UserID PRIMARY KEY(GroupID, TypeID),
);
GO
INSERT INTO Users (GroupID, TypeID, Name, Number)
VALUES
(1, 1, N'1 Владимир', 10),
(1, 3, N'1 Дмитрий', 0),
(2, 1, N'2 Андрей', 20),
(2, 3, N'2 Сергей', 0)
GO
Answer the question
In order to leave comments, you need to log in
UPDATE UserDestination
SET
Number = UserSource.Number
FROM
Users AS UserSource
INNER JOIN
Users AS UserDestination
ON
UserSource.GroupID = UserDestination.GroupID
AND UserSource.TypeID = 1
AND UserDestination.TypeID = 3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question