将运行,这将是一个普通的SQL语句:
select P.PartNum, M.Formula, M.RevisionNum
from Parts P
join Material M on P.PartNum = M.PartNum
where M.RevisionNum = (select max(M2.RevisionNum) from Material M2
where M2.PartNum = P.PartNum);
的重复以上关于修订#26后会发生什么的警告。根据#26之后发生的情况,最大(修订号)可能会中断。
编辑:
如果RevisionNum序列总是开始瓦特/ NEW,然后继续,A,B,C等,则MAX()需要更换瓦特/更复杂的东西(和凌乱):
select P.PartNum, M.RevisionNum
from Parts P
join Material M on P.PartNum = M.PartNum
where (
(select count(*) from Material M2
where M2.PartNum = P.PartNum) > 1
and M.RevisionNum = (select max(M3.RevisionNum) from Material M3
where M3.PartNum = P.PartNum and M3.RevisionNum <> 'NEW')
)
or (
(select count(*) from Material M4
where M4.PartNum = P.PartNum) = 1
and M.RevisionNum = 'NEW'
)
必须有更好的方法来做到这一点。这虽然有效 - 将不得不考虑更快的解决方案。
SQL小提琴:http://sqlfiddle.com/#!3/70c19/3
请注明你所使用的SQL Server版本。 – 2012-02-29 15:50:44
RevisionNum如何增加?它总是一个单一的字符?如果是这样,第27次修订会发生什么? – SupremeDud 2012-02-29 15:53:02
检查编辑。 – Jason 2012-02-29 16:43:56