Return number of rows as decimal - sql

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

Related

Not getting the division correct in postgresql 15 [duplicate]

Postgres is giving the wrong result for division when the denominator is greater than the numerator.
select 2/4 gives 0, select 4/2 works correct.
But select 2/4 should actually return 0.5
but it is returning 0 as integer
Postgres does integer division on integers. Ironically, it does not do integer averages on avg(), but that is a different matter.
One solution is simply converting one of the value to numeric:
select 2/4, 2::numeric/4
Postgres will add decimal places for numerics.
Try a floating point division, as in:
select 2.0 / 4 -- shows 0.5
Or:
select 1.0 * 2 / 4 -- shows 0.5

I have divided 2 column in sql lite and received a output. However, the decimals are too long and I want only 2 decimals. How do I get that?

Below is the query which I have used. I need the result only 2 decimals not more than that.
:sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 /
count(t.issue_resolved) AS finalscore
t.issue_resolved (1 column)
2022-03-19 80.97826086956522 - (I want this to be only decimals)
You can use round function
round(sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 / count(t.issue_resolved),2) AS finalScore

How do you calculate using sum in SQL Server

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.

SQL Server wrong mathematical result

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

'select count' when divided by another select count only returns when 1=1

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))