更新を現在のデザインに詰め込もうとする巧妙な(複雑な)方法を考え出すつもりはありませんが、テーブルのデザインを変更します。
BEGIN TRAN
CREATE TABLE Games (GameID INT PRIMARY KEY)
CREATE TABLE Players (PlayerID INT PRIMARY KEY)
CREATE TABLE PlayersGames (GameID INT, PlayerID INT, Position INT, conrti INT)
INSERT INTO Games VALUES (1), (2)
INSERT INTO Players VALUES (1), (2), (3), (4), (5)
INSERT INTO PlayersGames VALUES (1,1,1,0), (1,2,2,0), (1,3,3,0), (1,4,4,0), (1,5,5,0), (2,2,1,0), (2,1,2,0), (2,5,3,0), (2,3,4,0), (2,4,5,0)
ROLLBACK TRAN
今、あなたは次のUPDATE
ように簡単にすることができます
UPDATE PlayersGames SET contri = @YourValue WHERE PlayerID = 4 AND GameID = 1
編集
完全を期すために、質問に従って結果を表示することを含むスクリプト全体は次のようになります
BEGIN TRAN
CREATE TABLE dbo.Games (GameID INT PRIMARY KEY)
CREATE TABLE dbo.Players (PlayerID INT PRIMARY KEY)
CREATE TABLE dbo.PlayersGames (GameID INT, PlayerID INT, Position INT, contri INT)
ALTER TABLE dbo.PlayersGames ADD CONSTRAINT FK_PLAYERSGAMES_PLAYERS FOREIGN KEY (PlayerID) REFERENCES dbo.Players(PlayerID)
ALTER TABLE dbo.PlayersGames ADD CONSTRAINT FK_PLAYERSGAMES_GAMES FOREIGN KEY (GameID) REFERENCES dbo.Games(GameID)
INSERT INTO Games VALUES (1), (2)
INSERT INTO Players VALUES (1), (2), (3), (4), (5)
INSERT INTO PlayersGames VALUES (1,1,1,0), (1,2,2,3), (1,3,3,0), (1,4,4,0), (1,5,5,0), (2,2,1,0), (2,1,2,0), (2,5,3,0), (2,3,4,0), (2,4,5,0)
SELECT GameID
, Player1 = MIN(CASE WHEN Position = 1 THEN PlayerID ELSE NULL END)
, Player2 = MIN(CASE WHEN Position = 2 THEN PlayerID ELSE NULL END)
, Player3 = MIN(CASE WHEN Position = 3 THEN PlayerID ELSE NULL END)
, Player4 = MIN(CASE WHEN Position = 4 THEN PlayerID ELSE NULL END)
, Player5 = MIN(CASE WHEN Position = 5 THEN PlayerID ELSE NULL END)
, contri_P1 = MIN(CASE WHEN Position = 1 THEN contri ELSE NULL END)
, contri_P2 = MIN(CASE WHEN Position = 2 THEN contri ELSE NULL END)
, contri_P3 = MIN(CASE WHEN Position = 3 THEN contri ELSE NULL END)
, contri_P4 = MIN(CASE WHEN Position = 4 THEN contri ELSE NULL END)
, contri_P5 = MIN(CASE WHEN Position = 5 THEN contri ELSE NULL END)
FROM PlayersGames
GROUP BY
GameID
ROLLBACK TRAN