Round decimals for multiple averages and condensing avg? - sql

I was wondering if there was any way to condense this select statement?
as well as if it is possible to round each result to a certain amount of decimals?
It is set up correctly and giving me the right result but I was just wondering if there was any way to tighten it up? I am using SQLite
SELECT AVG(eFG),
AVG(OPP_eFG),
AVG(TOV_PCT),
AVG(OPP_TOV_PCT),
AVG(ORB_PCT),
AVG(DRB_PCT),
AVG(FTA_RATE),
AVG(OPP_FTA_RATE)

You can simply use the ROUND() function
SQLite round() function rounds a floating-point value t up to a number of digits to the right of the decimal point. If the 2nd argument (rounded digits) is omitted, it is assumed to be 0.
SELECT ROUND(AVG(eFG),2),
ROUND(AVG(OPP_eFG),2),
ROUND(AVG(TOV_PCT),2),
ROUND(AVG(OPP_TOV_PCT),2),
ROUND(AVG(ORB_PCT),2),
ROUND(AVG(DRB_PCT),2),
ROUND(AVG(FTA_RATE),2),
ROUND(AVG(OPP_FTA_RATE),2)

Related

Is my rounding formula off?

I'm using the below script to take numbers from my table and round them based on the value. If the number is above 15, I want the value to round off to one decimal place. If the number is below 15, I want the value to round off to three decimal places. The formula is working on numbers above 15, but will not place a ".0" next to whole values. I suspect that Access will not do this. Am I right? If there is a better way to do this please let me know.
SELECT Tbl_Formulas.QtyPer
, Round([QtyPer],IIf([QtyPer]<=15,3,1)) AS Expr1
FROM Tbl_Formulas;
You could use Format to display .0 or .000 even for integer values:
SELECT
Tbl_Formulas.QtyPer,
Format([QtyPer], IIf([QtyPer]<=15, "0.000", "0.0")) AS Expr1
FROM Tbl_Formulas;

How to use bigquery round up results to 4 digits after decimal point?

We don't have decimal data type in BigQuery now. So I have to use float
But
In Bigquery float division
0.029*50/100=0.014500000000000002
Although
0.021*50/100=0.0105
To round the value up
I have to use round(floatvalue*10000)/10000.
Is this the right way to deal with decimal data type now in BigQuery?
Depends on your coding preferences - for example you can just use simple ROUND(floatvalue, 4)
Depends on how exactly you need to round - up or down - you can respectively adjust expression
For example ROUND(floatvalue + 0.00005, 4)
See all rounding functions for BigQuery Standard SQL at below link
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#rounding-functions
Note that this question deserves a different answer now.
The premise of the question is "We don't have decimal data type in BigQuery now."
But now we do: You can use NUMERIC:
SELECT CAST('0.029' AS NUMERIC)*50/100
# 0.0145
Just make your column is NUMERIC instead of FLOAT64, and you'll get the desired results.
Rounding up in most SQL dialects is not a built-in function unless you're fortunate enough to be rounding up to an integer. In this case, CEIL is a quick and reliable solution.
In the case of rounding decimals up, we can also leverage CEIL, albeit with a couple of additional steps.
The procedure:
Multiply your value to move the last decimal to the tenths position. Ex. 18.234 becomes 1823.4 by multiplying by 100. (n * 100)
Use CEIL() to round up to the nearest integer. In our example, CEIL(n) = 1824.
Divide this result by the same figure used in step 1. In our example, n / 100 = 18.24.
Simplifying these steps leaves us with the below logic:
SELECT CEIL(value * 100) / 100 as rounded_up;
The same logic can be used to round down using the FLOOR function as such:
FLOOR(value * 100) / 100 AS rounded_down;
Thanks to #Mureinik for this answer.

postgresql round division

I want to divide values and round them up to 8 decimal places but i found that some divisions return in scientific notation.
How can i always get round division without scientific notation?
select round( 123/100000000::decimal, 8 )
returns 0.00000123 as expected.
select round( 1/100000000::decimal, 8 )
returns 1e-8 but... i was expecting 0.00000001
How can i round 1/100000000 to 8 decimal places and return 0.00000001 ?
sql fiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/1534
Thanks for help.
best,
Actually, when I try your code in Postgres 9.3.4 using pgAdmin, both return the values you want. The values are not in exponential notation. Hence, I suspect this is an issue with your application, not the database.
An easy way to check is to put the value as a string:
select round( 1/100000000::decimal, 8 )::text
This should not return exponential notation.
Probably second case go beyond precision range 8.
You can check this Question Selecting floating point numbers in decimal form
Also what version are you working with because in pgAdmin 9.2 I get a different result

rounding and converting value

I have some value with data type Numeric(28,10) (e.g. 128000,0000000000). I want to round it up to 2 significances and convert it into string. What is wrong with this?
convert(varchar,round(isnull(td2.Qty,0),2))
where td2.Qty is that value. It coverts it to string, but doesn't round it. Thanks in advance
It does round, but it keeps displaying the zeros because this is how numerics are always displayed.
If you need to stop displaying zeros, convert the value to a different type after the rounding, e.g. float or numeric(28,2):
convert(varchar, cast(round(isnull(td2.Qty,0),2) as numeric(28,2)))
SELECT CAST(round(isnull(128000,0000000000),2)AS FLOAT)
SELECT CAST(round(isnull(128000,0000000000),2)AS NUMERIC(28,2))
Let me explain the use of ROUND() function in SQL. It will not return the decimal value for the given length. E.g. round(isnull(td2.Qty,0),2) will not return friction value length 2, but rather
(e.g. 128000.0000000000) to (e.g. 128000.00)
instead of
(e.g. 128000.12340000000) to (e.g. 128000.120000000000).
It returns only after decimal 2 value and remain all '0'. In your case you want ROUND to truncate. so you can use
Cast(round(isnull(td2.Qty,0),2)as decimal(18,2));

Oracle ceil for decimal numbers

When rounding up to 2 decimal places, the value 4.01132141 would be rounded to 4.02 because it exceeds 4.01.
How can you do this in PL/SQL?
One way would be to do ceil(value*100)/100, but that seems inelegant. Not sure there's any way to make round behave the way you want.
The function to 'round up' is CEIL, but it generates an integer.
The function to 'round down' is FLOOR, but it too generates an integer.
The function to 'round nearest' is ROUND, and it allows you to specify a number of decimal places (dp).
Note that CEIL rounds to an integer; to round to 2 dp, you'd have to multiply by 100, use CEIL, and divide by 100.
To get the answer reasonably directly, use:
ROUND(value+0.005, 2)
This works because, for the example data of 4.01132141, the value passed to ROUND is 4.01632, and when rounded to 2 dp, that becomes 4.02. If the value started as 4.0593, say, then the value passed to ROUND would be 4.0643, which when rounded to 2 dp becomes 4.06, as required.
There are a couple of tricky bits there:
If the number of dp varies, the value to be added (0.005 in the example) varies. You could create a table to hold the number of decimal places in one column and the rounding value to add in the other. Alternatively, you could use an expression with powers of 10, etc.
Deciding on the correct behaviour for negative numbers. Does -4.01132141 become -4.02 or -4.01? You might need to play with SIGN and ABS functions to get that to work as you want.
I faced the same issue and came up with the following statement, it has worked fine so far.
select 4.01132141+(mod((ceil(4.01132141)-4.01132141)*1000,10)/1000) from dual
select 4.01132141, CEIL(4.01132141*100)/100 from dual
You can use this for Rounding up in PLSQL:
ROUND( UrNo+ (5 / POWER(10, DecimalPlaces + 1)) , DecimalPlaces)