snowflake Computation for dividing 2 columns giving wrong values - sql

I am exactly doing this Sum(2322933.99/1161800199.8)*
100
I should get
1.9 something but I am getting 64. Something
can anyone guide my y this division in snowflake giving wrong results
I tried them converting into decimal values and tried with Formula div0()
Nothing worked

I guess that your database table has 33 rows. So you get 33 * 1.9 (because of SUM), which is about 64.

My guess, with the few details that you gave us:
sum(x)/sum(y) is different than sum(x/y)
1/2 + 2/4 + 4/8 = 1.5
(1+2+4)/(2+4+8) = 0.5
Try writing sum(total gross weight)/sum(total cases filled) instead of sum(total gross weight /total cases filled).

Related

Snowflake CEIL Function - round up to next 0.1 kilometer

I have a column containing measurement values in meters.
I want to round them up (ceil) them to the next 100m and return it as a km value.
Special thing is: if the original value is a "round" number (100m increment) it should be ceiled up to the next 100m increment (see line 3 in the example below).
Example:
meter_value kilometer_value
1111 1.2
111 0.2
1000 1.1
I think I can get the first two lines by doing:
ceil(meter_value/1000,1) as kilometer_value
The solution I thought of to fix the edge case in line three is to just add 1 meter always:
ceil((meter_value+1)/1000,1) as kilometer_value
It seems a bit clumsy, is there a better way/alternative function to archive this?
You can check to see if it's divisible by 100 and only add one if it is:
ceil(((meter_value + iff(meter_value % 100 = 0, 1, 0))/1000), 1)
This will prevent situations where (if decimal parts are allowed) adding 1 to a value of 999.5 would not be accurate if adding one all the time.
Greg's answer is good, simpler to read to me would be to
divide by 100
floor
add 1
ceil
divide by 10
select
column1 as meter_value
,ceil(((meter_value + iff(meter_value % 100 = 0, 1, 0))/1000), 1) as greg
,ceil(floor(meter_value/100)+1)/10 as simeon
from values
(1111)
,(111)
,(1000)
,(1)
,(0)
;
METER_VALUE
GREG
SIMEON
1,111
1.2
1.2
111
0.2
0.2
1,000
1.1
1.1
1
0.1
0.1
0
0.1
0.1
do we want to mention negative values? I mean it distance, so it's a directionless magnitude, right?
anyway with negative value, both our methods the +1 forces the boundary case to be wrong.
Actually:
Once you have floored adding the 1 or 0.1 if you divide by 1000 vs 100 first, you don't need to ceil at all
thus two short forms can be:
,ceil(floor(meter_value/100)+1)/10 as version_a
,(floor(meter_value/100)+1)/10 as version_b
,floor(meter_value/1000,1)+0.1 as version_c

SQL Compound Growth Calculation based on previous rows (varying rate)

Given a column for 'Growth Factors' and a starting value I need to compute future values. For example, if a starting value of 1 is provided then the computed 'Value' column would be as shown below. Thus, Value(t2) = Value(t1) x Growth_Factor(t2). Base condition is Value(t1) = Starting_Value x Growth_Factor(t1). Example shown below.
How do I compute this in SQL (or Presto) where the computed value is dependent on previous computed values?
Growth Factor
Value
Time
1.2
1.2
1
1.1
1.32
2
1.5
1.98
3
1.7
3.366
4
You could sum the logarithms and invert when finished. This will work other than some possibility of small floating point error. But you're also going to introduce error once you multiply more than a few numbers with doubling decimal places at every iteration.
exp(
sum(ln(growth)) over (order by time)
)

How to handle decimal numbers in solidity?

How to handle decimal numbers in solidity?
If you want to find the percentage of some amount and do some calculation on that number, how to do that?
Suppose I perform : 15 % of 45 and need to divide that value with 7 how to get the answer.
Please help. I have done research, but getting answer like it is not possible to do that calculation. Please help.
You have a few options. To just multiply by a percentage (but truncate to an integer result), 45 * 15 / 100 = 6 works well. (45 * 15%)
If you want to keep some more digits around, you can just scale everything up by, e.g., some exponent of 10. 4500 * 15 / 100 = 675 (i.e. 6.75 * 100).

Percentage calculation in SQL Server

I have columns RSL and SUMofRSL. I have tried calculating the percentage but it returns either 100% or 0%. In some instances it is a wrong calculation since it shows 0% . Below are the examples for your reference.
RSL SUMofRSL Percentage
------------------------------
2 2 100%
1 2 0%
48 96 0%
10 10 100%
I have used
([RSL] / [SumOfRSL]) * 100
Assuming the data types of RSL and SumOfRSL are integers you will need to cast the columns to a data type that supports decimal places.
For example:-
(CAST([RSL] AS DECIMAL(10, 4)) / CAST([SumOfRSL] AS DECIMAL(10, 4)))
The division of two Integer factors will be another Integer. At least one of your factors must be decimal type if you want that the result be decimal. (See this link)
You can CAST one or both of your values as Kane suggested.

VB.Net About dividing Currency by an X amount of months

im trying to learn how to made stuff with currency.
For example:
I divide 10.000$ by 12 Months, rounding with 2 decimals i have 833,33 $.
If i multiply 833,33 $ * 12 i got 9999,96 $, so there is 0.04 of possible loss.
Rounding the 9999.96 with 2 decimals of presition i got 10.000 $ but that's what i don't want since 0.04 is a loss.
Im using SQL Compact 4.0 as database, the price_month table is decimal(18,2)
Here is my code:
Dim price as Decimal = 10000
Dim pricemonth as Decimal = Math.round((price/12),2) ' 833.33
Console.Writeline(pricemonth*12) ' 9999.96
Console.Writeline(Math.round((pricemonth*12),2)) ' 10000
Any advice how to increase accuracy with currency? Thanks and have a nice day!
Don't round your calculation. Leave the original numbers untouched but when you display the answer round it so that it looks nice.