I am trying something like this:
select sum(R_Value) ,((5/cast(sum(R_Value) as int)) * 100.000000000)
from R_Rating
where R_B_Id = 135
But I keep getting value 0, sum(R_Value) = 6, so I just want a percentage out of (5/sum(R_Value)) * 100.
How can I get this?
I have a rating of 5 so each user has a rating they can make select from 1 to 5, so what i need is a formula that takes in the sum and takes into account how many users have rated and give us a result from 1 to 5 in the end
Something like this may work but i need to round up to one decimal place to get a whole number.
select sum(R_Value), ((count(*)/cast(sum(R_Value) as float)) * 10)
from R_Rating
where R_B_Id = 135
To get the average rating you need to force floating point algebra. For example:
select 1.0 * sum(R_Value) / count(*)
from R_Rating
where R_B_Id = 135
Then, if your query selects three rows with the values: 1, 4, and 5, then this query will return 3.33 stars as the average. That is:
= 1.0 * (1 + 4 + 5) / 3
= 1.0 * 10 / 3
= 10.0 / 3
= 3.33333333
I recommend writing this as:
select sum(R_Value) ,
(500.0 / sum(R_Value))
from R_Rating
where R_B_Id = 135;
This avoids an integer division.
Related
Is there a way to make COUNT return a decimal?
For example:
SELECT 3 / 5 * 100 returns a value of 0
SELECT 3.0 / 5.0 * 100 returns a value of 60
If the COUNT results are 3 and 5, how can I make the results to store as 3.0 and 5.0?
To return a Decimal You can:
float(3.0 / 5.0 * 100)
If you want any variable to be float example
a = 9
print(float(a)
The result will be 9.0
Hope it help!
SELECT CAST(COUNT(*) AS DECIMAL(15,3)) AS DecimalCount
FROM MyTable
From the data in the first table how do I get a simple percentage of total calculation:
two / (two + three) = 2 / (2 + 3) = 0.4
to arrive at:
You can try the below -
select ((case when name='two' then value end)*1.0)/sum(value)
from t
where name in ('two','three')
You would rarely do something like this in SQL. Anyway, you can do:
with
two as (select value from t where name = 'two'),
three as (select value from t where name = 'three')
select 1.0 * two.value / (two.value + three.value) as answer
from two
cross join three
Result:
answer
----------------------
0.40000000000000000000
See running example at DB Fiddle.
Background:
My boss just send me a query with the following column
ROUND(SUM(ATR * QTDE) / SUM(QTDE), 2)
But then I thought isn't it the same as
ROUND(SUM(ATR), 2)
??
If it was just ATR * QTDE / QTDE I would be certain that it's the same, but with the SUM I'm not sure, looks the same, but I can't just use what I think it's the same without been certain of it. Also I don't want to question my boss about it, so... here I'm!
Question:
SUM(ATR * QTDE) / SUM(QTDE) is the same as SUM(ATR) ?
Wanted results:
Explanation and prove of why it's the same or it's different.
It isn't the same!
If you try with sample data you can see it ...
ATR QTDE
5 1
5 2
SUM(ATR) = 5 + 5 = 10
SUM(ATR * QTDE) / SUM(QTDE) = (5*1 + 5*2) / (1 + 2) = 15 / 3 = 5
No, they are not the same. One is a weighted average and the other is just a sum. It is easy to devise counter
examples.
ATR QTDE
1 100
2 200
SUM(ATR) = 3. The ratio returns 500 / 3 <> 3.
You are probably thinking that these are equivalent:
ROUND(SUM(ATR * QTDE) / SUM(QTDE), 2)
ROUND(AVG(ATR), 2)
That would only be true if SUM(QTDE) = COUNT(ATR) -- which would normally happen if your data is set up this way or if QTDE = 1.
I have this statement:
Select (30 - 5) * 700 / 30 as A , 700 - (5 * 700 / 30) as B
which has two ways to calculate the same equation.
These two equations should result in 583.33 if you made them by your calculator
but the previous statement results in 583 for field A, and 584 for field B.
Both are wrong and both are integer not decimal.
I want to know what's the right way to write this statement so I can get 583.33.
Thanks
Your expression is implicitly using an INT datatype. Try it this way to allow it to use a DECIMAL datatype
Select (30.000 - 5.000) * 700.000 / 30.000 as A ,
700.000 - (5.000 * 700.000 / 30.000) as B
Use Cast function:
As next:
Select Cast((30 - 5) * 700 as DECIMAL(19,2) ) / 30 as A ,
700 - ( Cast (5 * 700 as decimal (19,2 )) / 30) as B
Result:
A B
583.333333 583.333334
I'm basically trying to get the percent of two select counts. In the example shown below, I should have a simple 2 / 6 which results in 33.33%. However when I run the query is only returns either 100% or 0%. Any help on why it does what it does would be fantastic
CONVERT(DECIMAL(4, 2),
(SELECT Count(driver)
FROM trh a
WHERE a.hn = trh.hn
AND a.driver = trh.driver
AND (a.finishposition < 5
AND a.finishposition IS NOT NULL
AND a.finishposition != 0 )) / Count(driver) ) * 100
Now that I try CONVERT(DECIMAL(4, 2), 2 / 6), this just returns 0.00 also
Okay you have to cast them as decimals
SQL Server does integer division. Just cast() one of the values to a decimal or floating type. For instance:
/ cast(Count(driver) as decimal(4, 2))