Split values in Millions,Thousands,Units in spark sql - sql

How to get a value split in Billions,millions,thousands,Units and decimals in spark sql
At the moment I tried the below options
Thousands - ABS(Amount)%1000000 Output : 562191.7974663
Units - ABS(Amount)%1000 Output : 191.7974663
Decimals - ABS(Amount)%1 Output : 0.7974663 or -0.56724
However, I am looking for an output as per below table.
Amount Amt_Billions Amt_Millions Amt_Thousands Amt_Units Amt_Decimals
3946562191.7974663 3000000000 946000000 562000 191 7974663
-743245613.56724 743000000 245000 613 56724
Please can anyone help me with this calculation.
Thanks in advance.

you can get the orders of magnitude like so.
floor(abs(Amount)%1000000000000/1000000000)*1000000000 Billions,
floor(abs(Amount)%1000000000/1000000)*1000000 Millions,
floor(abs(Amount)%1000000/1000)*1000 Thousands,
floor(abs(Amount)%1000) Units
Converting the decimal portion I think you will have to resort to a string expression, ie cast to string, find the index of the "." then take the right(string,len(string)-index), finally cast to int, unless it's just for display purposes of course.

Hmmm . . .
floor(amount / 1000000000) * 1000000000 as billions,
floor((amount % 1000000000) / 1000000) * 1000000 as millions,
floor((amount % 1000000) / 1000) * 1000) as thousands,
floor(amount % 1000) as units
The decimals are the only tricky ones. The numbers don't make sense, because 0.1, 0.01, 0.001 would all resolve to 1. I would suggest doing this in some units, such as millions:
floor((amount % 1) * 1000000) as units

Related

Filter numeric column containing a digit

Is there any way to filter rows of a table where a numeric column contains a digit using maths?
I mean, currently, I'm solving that using:
where cast(t.numeric_column as varchar(255)) like "%2%"
However, I would like to know if could be possible to filter apply numeric operations...
Any ideas?
You could use division plus the modulus, if you knew the range of possible numbers. For example, assuming all expected numbers were positive and less than 100,000, you could use:
SELECT *
FROM yourTable
WHERE numeric_column % 10 = 2 OR
(numeric_column / 10) % 10 = 2 OR
(numeric_column / 100) % 10 = 2 OR
(numeric_column / 1000) % 10 = 2 OR
(numeric_column / 10000) % 10 = 2;
Although the above is ugly and unwieldy, it might actually outperform your approach which requires a costly conversion to string.

How to get value in percentage (%) or pixel (px) after doing some maths? | Lesscss

I've a little and simple question regarding Lesscss. How I can get calculated value in percentage or pixel in Lesscss. Like, I have this : ((1 / 1) * 1) = 1. I know the answer is 1, but I want this "1" to be in percentage or pixel like this: "1%" or "1px".
I just need to insert or put percentage (%) or pixel (px) sign in the calculated value.
I'd appreciate the help.
use:
unit(#yourvalue,px)
or
unit(#yourvalue,~"%")
read more here
Additional note:
by default the first occurring unit in the calculation will be assigned to the result. For example unit((1 / 2 * 3),px); and (1px / 2% * 3rem) will both return 1.5px.
I got the solution. What's required is to simply multiply (1%) or (1px) with the calculated value.
For Percentage: ((1 / 1) * 1) * 1% = 1%
For Pixel: ((1 / 1) * 1) * 1px = 1px
Good Luck! (Y).

ios issue with log calculation

I am working on a calculation for free space loss and hitting a snag.
Doing this calculation:
fslLoss = 36.6 + (20 * log(fromAntenna/5280)) + (20 * log(serviceFreq))
Where fslLoss is a float and fromAntenna and servicefreq are integers:
NSLog(#"the freespace Loss is %0.01f", fslLoss);
The result is "the freespace Loss is -inf"
The issue appears to be in the 20log(fromAntenna/5280) section, as I get normal results without it.
BTW ... tried log10 with the same results.
Thanks for the help,
padapa
You say fromAntenna is an integer, so fromAntenna/5280 will be calculated with integer arithmetic. That means it will be rounded (floored, technically), probably not what you intended.
Fix it with:
log( (double) fromAntenna / 5280.0 )
log(0) is -inf. The integer division inside the logarithm may be zero. Use fromAntenna/5280.0 to get float division.
The compiler is correctly using fromAntenna & serviceFreq as ints and that's not giving you good results when fslLoss is a float. Use some float casts and you'll have better luck:
fslLoss = 36.6 + (20 * log((float)fromAntenna/5280)) + (20 * log((float)serviceFreq));

Time to turn 180 degrees

I have a space ship, and am wanting to calculate how long it takes to turn 180 degrees. This is my current code to turn the ship:
.msngFacingDegrees = .msngFacingDegrees + .ROTATION_RATE * TV.TimeElapsed
My current .ROTATION_RATE is 0.15, but it will change.
I have tried:
Math.Ceiling(.ROTATION_RATE * TV.TimeElapsed / 180)
But always get an answer of 1. Please help.
To explain why you get 1 all the time:
Math.Ceiling simply rounds up to the next integer, so your sum contents must always be < 1.
Rearranging your sum gives TV.TimeElapsed = 180 * (1/.ROTATION_Rate). With a ROTATION_Rate of 0.15 we know that TV.TimeElapsed needs to reach 1200 before your overall function returns > 1.
Is it possible that you're always looking at elapsed times less than this threshold?
Going further to suggest what your sum should be is harder - Its not completely clear without more context.

Objective-C Integer Arithmetic

I'm trying to calculate some numbers in an iPhone application.
int i = 12;
int o = (60 / (i * 50)) * 1000;
I would expect o to be 100 (that's milliseconds) in this example but it equals 0 as displayed by NSLog(#"%d", o).
This also equals 0.
int o = 60 / (i * 50) * 1000;
This equals 250,000, which is straight left-to-right math.
int o = 60 / i * 50 * 1000;
What's flying over my head here?
Thanks,
Nick
In Objective-C / performs integer division on integer arguments, so 4/5 is truncated to 0, 3/2 is truncated to 1, and so on. You probably want to cast some of your numbers to floating-point forms before performing division.
You're also running in to issues with precedence. In the expression
60 / (i * 50) * 1000
the term inside the parentheses is calculated first, so 60 is divided by 600 which produces the result 0. In
60 / i * 50 * 1000
the first operation is to divide 60 by 12 which gives the result 5 and then the multiplications are carried out.
An integer divided by an integer is an integer.
so 60/600 is not 0.1, it is 0.
Cast (or declare) some stuff as float instead.
It's doing integer math. 60 / (12 * 50) is 0.1, truncates to 0.
Should work if you force floating point and then cast back to an integer.
int o = (int)(60.0 / ((double) i / 50.0) * 1000.0;
Probably not really necessary to make everything a double.
Replace:
int o = (60 / (i * 50)) * 1000;
with:
int o = 1200/i;
By order of precedence, the operation:
60 / (12 * 50)
is performed before multiplying by 1000.
This value is less than 1 and is cast to an int, which truncates it to 0. And 0 times anything is 0.
Use a float or first multiply by 1000 to ensure you're not ending up with propagating a 0 in your calculations.
All the operations in your expression are performed in integer arithmetic, meaning that the fractional part of each intermediate result is truncated. This means that if you divide a smaller integer by a larger integer you will always get 0.
To get the result you want you must either make sure the operations are performed in a particular order, or you must use floats. For example the result of
int o = (60.0 / (i * 50.0)) * 1000.0;
should be o = 100.
I think you need to use float here instead of int. It will work the way you want! Will give you answer in decimals as well.