How does dual work? I have executed this with many values Select Round(45.926,-1) from dual it prints 50 while
Select Round(45.926,-2) from dual prints 0.
When using ROUND function, the second parameter represents the places after decimal to be taken in result. So, following are results:
SELECT ROUND(45.926) FROM dual;
Result: 46 because no places after decimal to be considered.
SELECT ROUND(45.926, 1) FROM dual;
Result 45.9, consider 1 place after decimal
SELECT ROUND(45.926, -1) FROM dual;
Result: 50, considering the value to be 4.5926, rounding it to 5 and then printing the result.
SELECT ROUND(45.926, -2) FROM dual;
Result: 0, considering the value to be 0.45926, rounding it to 0 and then printing the result in original 10x which is 0x100 = 0.
If you'd taken,
SELECT ROUND(55.926, -2) FROM dual;
Then result will be 100 because it would be like, rounding 0.55926 to 1 and then 10x which 1x100 = 100.
Round(n,m) - rounds the number n to m decimal places.
For m = -1, it rounds the number to tenth place making 45.926 to 50 as it is greater than 45
For m = -2, it rounds the number to 100 place. Since 45.926 is less than 50, it becomes 0. If you try round(51,-2) you should get 100
Related
I have an age column which calculates age for each member in my report.The output is a whole number followed by a decimal point and numbers. I would like the first number only right after the decimal point .
I tried trunc but it gives me everything before the decimal and then the number I want after .Then I tried to trunc with a call out with a comma and it doesnt work.
trunc(age,',')
Example -
age 15.7
expected output 7
Here the mathematical answer
take the decimal part by susbriacting the whole part (trunc).
multiply by 10 and take the whole part
.
with age as (select 15.7231 age from dual)
select trunc(10*(age-trunc(age))) dp1 from age
DP1
----------
7
try like below
select substr(to_char(15.7,'9999.0'),-1,1) as col from dual
it will return 7
Multiply by 10, trunc it and take the remainder of the division by 10.
with age as (select 15.7231 age from dual)
select mod(trunc(10*age), 10) dp from age
Output:
DP
--
7
Plain and simple, does anybody know why this:
Select 30 * 220 / 30
Returns 220, which is the correct result, and this:
Select 30 * (220/30)
Returns 210???
On the second case, I realise that 220/30 is being calculated first, generating a decimal (7,333333...) but still... isn't this lousy precision?
Under integer division 220/30 = 7 and 99/100 = 0 (note truncation not rounding)
Use non integers to avoid this. e.g. Select 30 * (220/30.0)
Or you can use an explicit cast
Select 30 * (220/cast (30 as float))
The one in the parentheses, is always evaluated first, but since the machine logic you are using integer, in that case, the result of the division is 7, wich you multiply by 30, gives you 210
I have a table with two columns, number of maximum number of places (capacity) and number of places available (availablePlaces)
I want to calculate the availablePlaces as a percentage of the capacity.
availablePlaces capacity
1 20
5 18
4 15
Desired Result:
availablePlaces capacity Percent
1 20 5.0
5 18 27.8
4 15 26.7
Any ideas of a SELECT SQL query that will allow me to do this?
Try this:
SELECT availablePlaces, capacity,
ROUND(availablePlaces * 100.0 / capacity, 1) AS Percent
FROM mytable
You have to multiply by 100.0 instead of 100, so as to avoid integer division. Also, you have to use ROUND to round to the first decimal digit.
Demo here
The following SQL query will do this for you:
SELECT availablePlaces, capacity, (availablePlaces/capacity) as Percent
from table_name;
Why not use a number formatting function such as format_number (or an equivalent one in your database) to format a double as a percentage? This example is generalized. The returned value is a string.
WITH t
AS
(
SELECT count(*) AS num_rows, count(foo) as num_foo
FROM mytable
)
SELECT *, format_number(num_foo/num_rows, '#.#%') AS pct_grade_rows
FROM t
This avoids the use of round and multiplying the numerator by 100.
I have the following query, in which I'm attempting to work out percentiles for the days between a letter being sent and today's date:
SELECT PERCENTILE_DISC(0.1) WITHIN GROUP (ORDER BY SUM(TRUNC(SYSDATE) -
( TO_DATE( SUBSTR(M.LETTER_SENT, 1, 11), 'YYYY-MON-DD') )) ASC) AS PERCENTILE_10,
PERCENTILE_DISC(0.9) WITHIN GROUP (ORDER BY SUM(TRUNC(SYSDATE) -
( TO_DATE( SUBSTR(M.LETTER_SENT, 1, 11), 'YYYY-MON-DD') )) ASC) AS PERCENTILE_90
FROM MV_TABLE M8
WHERE M8.LETTER_SENT != 'N'
GROUP BY M8.LETTER_SENT;
I am perhaps wrong in thinking that, it should return the 10th and 90th percentile for the result set?
M.LETTER_SENT is in the format YYYY-MON-DD: USER_ID. So my query uses SYSDATE - TO_DATE(SUBSTR(M.LETTER_SENT,1, 11), 'YYYY-MON-DD') to work out the number of days between.
So the M.LETTER_SENT actual value for the result set I list below of 4 days 2015-Feb-27: rstone
That query returns the following result set:
242
4
4
4
39
11
18
361
My understanding of percentiles is that if you want the 90th percentile the following occurs.
number of records * percentile = percentile => (round up) = index value
So in my situation it's:
8 * 0.1 = 0.8 => (round up) = 1
8 * 0.9 = 7.2 => (round up) = 8
The 1st value on the ordered result set is: 4
The 8th value on the ordered result set is: 361
Oracle for me returns: 11 as the 10th percentile though?
When I do 0.2 + 0.8 percentiles I get 12, 242 respectively. I always understood there to be a few different ways to calculate percentiles. So how does Oracle calculate these results am I wrong in my thoughts of what the percentiles should be?
Is trunc and round the same with negative arguments?
SQL> select round(123456.76,-4) from dual;
ROUND(123456.76,-4)
-------------------
120000
SQL> select trunc(123456.76,-4) from dual;
TRUNC(123456.76,-4)
-------------------
120000
No, behavior depends on the value of the significant digit (the 3rd digit (the 3) is the significant one in your case, as it is below 5 round and trunc do the same )
try select trunc(125456.76,-4) from dual (result is 120000) vs select round(125456.76,-4) from dual (result is 130000). Now when the significant digit is 5 (or higher) the results of trunc and round differ.
ROUND is related to round figure of given value.
TRUNC is related to truncation of given values.
In round case of given example, four places till 4th place prior to decimal point padded with 0.
But in trunc case, four places till 4th place prior to decimal point replacd with 0.
TRUNC - depending on second parameter it returns specified decimal places that is specified in second argument. Ex:
trunc(25.656) -- 25
trunc(25.656, 1) -- 25.6
trunc(25.656, 2) -- 25.65
ROUND - round off the given number to the nearest value. Ex:
round(66) -- 70
round(64) -- 60
round(65.7) -- 66
round(65.3) -- 65