Jika SQL Server 2005+
;WITH m AS
(
SELECT Partnum, Formula, RevisionNum,
rn = ROW_NUMBER() OVER (PARTITION BY PartNum ORDER BY
CASE WHEN RevisionNum ='New' THEN 1 ELSE 2 END)
FROM dbo.Material
)
SELECT p.PartNum, m.Formula, m.RevisionNum
FROM dbo.Parts AS p
INNER JOIN m ON p.PartNum = m.PartNum
WHERE m.rn = 1;
Meskipun penasaran, apa yang Anda lakukan ketika ada lebih dari 26 revisi (misalnya apa yang muncul setelah Z
)?