Converting decimal values to non decimal in computed column in oracle sql - sql

I am trying to convert decimal values into non decimal number for a computed column in Oracle SQL.
Please see the below code. But I am not able to get the desired output.
Query:
Select cast(cost_account)*100/cast(amnt_fin) as "computed_LTV"
From Loan_app.
Here I Want a new column name as computed_LTV with the required calculation with no decimal in the output.

It is ROUND you're looking for, I presume.
SELECT ROUND (cost_account * 100 / amnt_fin) AS "computed_LTV" FROM Loan_app

If by "non-decimal number" you mean "integer", just use trunc():
Select trunc(cost_account * 100 / amnt_fin) as computed_LTV
From Loan_app;

Related

SQL Division precision

I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html

Oracle SQL: Returning sum query results as a decimal with precision

I see that I need to return the sum of a column in Oracle for which the query I use is the following
select sum(scores) from table_x;
I want to return the results always like this
If the result is an integer, say 1000, the output should be 1000.00 (by default 2 precision digits for the fractional part)
If the result is a decimal with n digits precision, the result will should be returned as it is for example, if sum = 1000.555666, then I want the result to be 1000.555666.
The problem is when I modify the query to use cast like
select cast(sum(scores) as binary_double) from table_x
When the sum is 1000, I get the result as 1000.0, but I want one more zero in the fractional part (1000.00) since I do string comparison of results. Tried numerous solutions like casting to different data types, but can't get the integer results in the form I want.
Try:
select to_char( sum(scores), 'FM9999999999D009999' )
from table_x;
Please refer to the documentation of to_char function for details ==> click

Use Group By for real or float column with specify precision

How can I specify precision for a column of the REAL type (eg. to three digits after the decimal point) for Group By clause?
Eg. 12.023006 is equal to 12.023007 for 3 digits precision after the decimal point and should be grouped by Group By.
A couple ways you can achieve this, one of which is to CAST your field to the appropriate precision:
select cast(somecol as decimal(10,3))
from sometable
group by cast(somecol as decimal(10,3))
SQL Fiddle Demo
You could also use the ROUND function to achieve this.

AVG() not accurate in SQL Server

I am trying to calculate an Avg() for an column with where condition using SQL Server Management Studio 2008. When I try the AVG(column-name) in a SQL query it gives me a rounded value and not a decimal value.
When I copy the dataset into MS Excel I get the accurate value with 4 decimal (example: 10.74) in SQL I am getting just 10. Any help is appreciated.
My query:
SELECT Item, AVG(POOrdrQty)
FROM [tableWR]
where Item ='737' AND POOrdrQty <((select AVG([POOrdrQty]) FROM [tableWR]) * 2)
group by Item
The resulting type of the avg aggregate is the same type as the value you use as parameter. If the field is an int, the result will be rounded to fit the type.
Cast the value that you use as parameter: avg(cast(column-name as float)).
avg(column_name * 1.0) to convert data type to float

sql server round function not working well

I am using sql server 2000 and facing round function issue like the following statement working fine.
SELECT ROUND(5 * 7.83, 1)
The result will be 39.2
But when I get these values from the table, it gives 39.1, meaning it truncates and does not round up.
SELECT ROUND(rate * qty, 1)
FROM tbl
The result will be 39.1
rate and qty columns data types are float. Insert 5 in qty and 7.83 in rate, then check it. How I can fix it?
Convert the table values to real,
SELECT ROUND(convert(real,rate)*convert(real,qty),1)
Your sample simply query is not reflective of the data types involved.
Try these two instead:
SELECT ROUND(5 * 7.83, 1)
SELECT ROUND(cast(5 as float) * cast(7.83 as float), 1)
The 2nd one matches your table data types. Float datatypes are not meant for precise decimal calculations, use a decimal type for those instead.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Without losing too much precision for normal numbers, you can just cast to decimal on the fly to force human-comprehensible decimal arithmetics, e.g.
SELECT ROUND(cast(rate as decimal(10,5)) * cast(qty as decimal(10,5), 1)
FROM tbl