Last active
June 15, 2016 15:45
-
-
Save chrispickford/6a5b2369b91a57a13c628dc52b1e854f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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