免责声明:
- 查询语言:Microsoft SQL 查询
- 步骤在以下评论中得到澄清:-
问题:用“D”更新最后一个 ID,其中年、月是重复的成员。
-- Sample Table Creation
DECLARE @TempTable TABLE
( Id INT IDENTITY (1, 1) NOT NULL, MemberId INT, for_year INT, for_month INT, for_status CHAR(1) )
-- Sample Insert
INSERT INTO @TempTable
SELECT '2', '2077', '11', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '11', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '77', '2076', '10', 'A' UNION ALL
SELECT '77', '2076', '11', 'A' UNION ALL
SELECT '77', '2076', '12', 'A'
-- where expectation meets reality :) -- logic / main query
UPDATE @TempTable SET for_status = 'D' WHERE Id IN
(
SELECT MAX(Id) FROM @TempTable GROUP BY MemberId, for_year, for_month HAVING COUNT(*)>1
)
-- Sample Select
SELECT * FROM @TempTable