Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query multiplying SUM result by amount of inner elements

I have a query that lists features, based on components status and availability

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 follow:

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 of

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 have respectively 8, 8 and 7 models. Which means that my query is doing for all of them to calculate priority: Impact*models + Visibility*models + Risk*models.

How can I get my SQL query to not do that multiplication?

Thanks

like image 889
LanFeusT Avatar asked Apr 26 '12 23:04

LanFeusT


People also ask

How do I multiply all values in a column in SQL?

So now our function looks like this e(logx+logy) where log x + log y =log(x * y) which gives the product of all the values in the column which have been computed to the log values.

How do you multiply values in SQL?

The SQL multiply ( * ) operator is used to multiply two or more expressions or numbers.

Can we use count inside sum in SQL?

SUM() and COUNT() functions SUM of values of a field or column of a SQL table, generated using SQL SUM() function can be stored in a variable or temporary column referred as alias. The same approach can be used with SQL COUNT() function too.

How do you write a multiplication aggregate function in SQL?

You asked the number to EXP, and you EXP with that number. Combine that with the fact that x^a * x^b = x^(a+b) - which is easy to understand as well: x^a means "multiple x by itself, a times". Now multiply that by x, b times.


1 Answers

Based on your comment and unless I am not understanding what you need, I don't think you need the SUM(). I think you just want the total for each model. Using the SUM() you are going to get the total for all records which it doesn't sound like you want. This would also remove your GROUP BY and HAVING

SELECT Brand.Id
  , Brand.Name
  , Brand.Impact
  , Brand.Visibility
  , Brand.Risk
  , (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)
  AND (Brand.YearId = @YearId)
like image 116
Taryn Avatar answered Oct 27 '22 00:10

Taryn