Skip to content

Instantly share code, notes, and snippets.

@chrispickford
Last active June 15, 2016 15:45
Show Gist options
  • Save chrispickford/6a5b2369b91a57a13c628dc52b1e854f to your computer and use it in GitHub Desktop.
Save chrispickford/6a5b2369b91a57a13c628dc52b1e854f to your computer and use it in GitHub Desktop.
DECLARE @table TABLE (Rate INT, MatID INT);
INSERT INTO @table
(Rate, MatID)
VALUES (1, 81),
(2, 82),
(2, 83),
(3, 85),
(2, 86),
(2, 87),
(3, 88);
SELECT Rate,
MatID
FROM @table;
;
WITH CTE_MinMatID
AS (
SELECT Rate,
MIN(MatID) MinMatID
FROM @table
GROUP BY Rate
)
UPDATE t
SET t.MatID = cte.MinMatID
FROM @table AS t
INNER JOIN CTE_MinMatID cte ON cte.Rate = t.Rate;
SELECT Rate,
MatID
FROM @table;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment