Not getting the division correct in postgresql 15 [duplicate] - sql

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

Related

Weird decimal values obtained through SQL type casting

I have a very peculiar situation happening while doing a simple division using GoogleSQL.
For example, variables involved:
Fruit_name -- String
price_purchased -- String
used_discount -- double
Example values:
fruit_name
price_purchased
used_discount
apple
5
0.8
pear
6
0.76
A simple division through SQL to find out original price:
SELECT DISTINCT
fruit_name,
CAST(price_purchased AS double),
used_discount,
CAST(price_purchased AS double) / (1 - used_discount) as original_price
FROM
fruit_table
I'm weirdly getting such a result:
fruit_name
price_purchased
used_discount
original_price
apple
5
0.8
25.000000000000007
pear
6
0.76
25
The original price for the apple should also give me a value of 25 exactly, just like how the pear's value was also at 25.
How is it that the value of the apple's original price could have that weird minuscule amount? Is this due to some type casting issue?
Thanks.
The division result is floating point value, you need to it to NUMERIC type:
SELECT
CAST(5 AS FLOAT64) / (1 - 0.8) AS float_original_price,
CAST(CAST(5 AS FLOAT64) / (1 - 0.8) AS NUMERIC) AS numeric_original_price
float_original_price
numeric_original_price
25.000000000000007
25
You can refer to the docs for Bigquery Numeric Types. Also, check this post : What is the difference between NUMERIC and FLOAT in BigQuery?

PostgreSQL 10.1 incorrect division output

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

Redshift division result does not include decimals

I'm trying to do something really quite basic to calculate a kind of percentage between two columns in Redshift. However, when I run the query with an example the result is simply zero because the decimals are not being covered.
code:
select 1701 / 84936;
Output:
I tried :
select cast(1701 / 84936 as numeric (10,10));
but the result was 0.0000000000.
How could I solve this silly thing?
It is integer division. Make sure that at least one argument is: NUMERIC(accurate data type)/FLOAT(caution: it's approximate data type):
/ division (integer division truncates the result)
select 1701.0 / 84936;
-- or
SELECT 1.0 * 1701 / 84936;
-- or
SELECT CAST(1701 AS NUMERIC(10,4))/84936;
DBFiddle Demo
When mixing data types the order counts
Note that the order of the elements in a math expression counts for the data type of the result.
Let's assume that we intend to calculate the percentage unit_sales/total_sales where both columns (or numbers) are integers.
See and try with this code here.
-- Some dummy table
drop table if exists sales;
create table sales as
select 3 as unit_sales, 9 as total_sales;
-- The calculations
select
unit_sales/total_sales*100, --> 0 (integer)
unit_sales/total_sales*100.0, --> 0.0 (float)
100.0*unit_sales/total_sales --> 33.3 (float and expected result)
from sales;
The output
0 | 0.0 | 33.33
The first column is 0 (integer) because of 3/9=0 in an integer division.
The second column is 0.0 because SQL first got the integer 0 (3/9), and later, SQL converts it to float in order to perform the multiplication by 100.0.
The expected result.
The non-integer 100.0 at the beginning of the expression force a non-integer calculation.

Rounding up with two decimal places usind SQL

I want to a number like 3.5212, becomes 3.53. But using:
select Ceiling(3.5212)
it returns 4.
Can I make it always round up with two decimal places?
One way could be to multiply the value by 100 before ceiling function and then divide the final value by 100 as below.
select Ceiling(3.5212 * 100) / 100
Result:
VAL
--------
3.530000
DEMO

TSQL Sum is giving wrong result

I want to get sum of data but I am getting wrong result
Example 1
Example 2
Result when doing sum
AS you can see from Example 1 - where p_key is 11020145101617761 and LC_Amount is 8.4 , 168 , -176.4 the sum of this is 0
similarly in Example 2 - where p_key is 1102014510615767 and LC_amount is
-571067.53, 543873.84 , 27193.69 the sum of this is also 0
but in the result when I do group by with p_key , I am not getting 0
I don't understand what is the reason behind this.
It's an example of IEEE-754 rounding errors. Note the numbers are all very close to zero, but juuuuuust off, see the exponent.
Wrap your SUM in ROUND():
SELECT ROUND( SUM( LC_Amount ), 10 )
...should do it.
This occurs because float is not true exact numeric type.
Try to use DECIMAL(10,2) type to avoid round errors.
There is a lot info about float type in the web.