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
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.
The SQL multiply ( * ) operator is used to multiply two or more expressions or numbers.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With