Rockmoose's works if the project and drawing doesn't change for the whole table--Your table ----------------------------------------------------set nocount ondeclare @tb Table (Project char(4), Drawing char(3), Revision int, Weight int)insert @tb Select 'A001', '001', 0, 10 union allSelect 'A001', '001', 1, 11 union allSelect 'A001', '001', 2, 12 union allSelect 'A001', '001', 2, 13 union allSelect 'A001', '001', 2, 12 union allSelect 'A001', '001', 3, 33 union allSelect 'A001', '001', 4, 22 union allSelect 'A001', '001', 4, 21 union allSelect 'A001', '001', 4, 22--Your table ----------------------------------------------------declare @tb2 Table (Project char(4), Drawing char(3), Revision int)insert @tb2Select Project ,Drawing ,max(Revision) revisionFrom @tbGroup by Project ,DrawingSelect b.Project ,b.Drawing ,b.revision ,sum(b.weight) [sumweight]From @tb2 aJOIN @tb b ON a.Project = b.Project and a.Drawing = b.Drawing and a.revision = b.revisionGroup by b.Project ,b.Drawing ,b.revision-------------------------------------------With derived table (unless you consider this a subquery)Select b.Project ,b.Drawing ,b.revision ,sum(b.weight) [sumweight]From ( Select Project ,Drawing ,max(Revision) revision From @tb Group by Project ,Drawing ) as aJOIN @tb b ON a.Project = b.Project and a.Drawing = b.Drawing and a.revision = b.revisionGroup by b.Project ,b.Drawing ,b.revision
Be One with the OptimizerTG