下面的sql腳本會將任何未分類更新爲「實際」信用評級。我希望這有幫助。
CREATE TABLE #WorkTable
(
InstrID INT,
CountryName VARCHAR(50),
CreditRating VARCHAR(20)
)
INSERT INTO #WorkTable VALUES (1, 'UK','AA');
INSERT INTO #WorkTable VALUES (2, 'UK','Unclassified');
INSERT INTO #WorkTable VALUES (3, 'South Africa','A');
INSERT INTO #WorkTable VALUES (4, 'South Africa','A');
INSERT INTO #WorkTable VALUES (5, 'South Africa','Unclassified');
WITH cteUnclassified
AS
(
SELECT InstrID,
CountryName,
CeditRating
FROM #WorkTable
WHERE CreditRating != 'Unclassified'
)
UPDATE #WorkTable
SET CreditRating = u.CreditRating
FROM #WorkTable wt
INNER JOIN cteUnclassified u
ON wt.CountryName = u.CountryName
WHERE wt.CreditRating = 'Unclassified'
SELECT *
FROM #WorkTable
下面的查詢的結果:
InstrID國家或地區名稱企業資信 1 UK AA 2 UK AA 3南非甲 4南非甲 5南非甲