I have a query that lists functions based on the status and availability of components.
SELECT Brand.Id , Brand.Name , Brand.Impact , Brand.Visibility , Brand.Risk , SUM(Brand.Impact + Brand.Visibility + Brand.Risk) AS Priority FROM Brand LEFT OUTER JOIN Type ON Brand.Id = Type.FeatureId LEFT OUTER JOIN Model ON Type.Id = Model.SubFeatureId LEFT OUTER JOIN TestingStatus ON Model.StatusId = TestingStatus.Id WHERE (Model.IsDeleted = 'False') AND (Brand.IsDeleted = 'False') AND (Type.IsDeleted = 'False') AND (@TeamId = 0 OR Model.TeamId = @TeamId) GROUP BY Brand.YearId, Brand.Id, Brand.Name, Brand.Impact, Brand.Visibility, Brand.Risk HAVING (Brand.YearId = @YearId)
My results are as follows:
ID Name Impact Visibility Risk Priority 403 Chevy 1 2 3 48 404 Nissan 1 1 1 24 405 Toyota 3 2 1 42
Instead
ID Name Impact Visibility Risk Priority 403 Chevy 1 2 3 6 404 Nissan 1 1 1 3 405 Toyota 3 2 1 6
Each of these brands has 8, 8 and 7 models, respectively. This means that my query performs a priority calculation for all of them: Impact*models + Visibility*models + Risk*models
.
How can I make my SQL query not do this multiplication?
thanks
source share