Possible to round down to second decimal place based off 3+ decimal spots in MySQL - sql

I am trying to figure out how to essentially create a "floor" call based on a specific decimal place as opposed to a whole value. Below is a table of actual values and the desired result:
=========|=========
3.125 | 3.12
4.187 | 4.18
1.212 | 1.21
5.999 | 5.99
Is this possible with mysql? using the round function to the 2nd decimal place returns "bad" data and rounding to the third does not reach the goal either.

Use the TRUNCATE function:
SELECT TRUNCATE(3.125, 2)
Output:
3.12

Could you multiply by 100 and floor and then divide by 100? Like
floor(value*100)/100

Related

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)
)

Filter the Google Finance formula to only display the "high" of all time

It's in reference to the Google Finance function in Google Sheets: https://support.google.com/docs/answer/3093281?hl=en
I would like to obtain the "all time LOW" (ATL) and "all time HIGH" (ATH) for a specific ticker (i.e. ABBV or GOOG) but only in 1 cell for each. Basically "What's the ATL/ATH value for this ticker?"
I've tried to do both formulas for ATL and ATH, but only ATL gives the expected result for now.
To get the ATL, you can use
=GOOGLEFINANCE("ABBV","low","01/12/1980",TODAY(),7)
and to get the ATH you can use:
=GOOGLEFINANCE("ABBV","high","01/12/1980",TODAY(),7)
The output of this is 2 columns of data:
Please note that column A, containing the timestamp, will be the one making trouble when it comes to computing the MAX function as it translates into some weird figures.
In order to get the ATL, I'll be using the MIN function which works perfectly fine:
=MIN(GOOGLEFINANCE("ABBV","low","01/01/1980",TODAY(),7))
as it will just scan the 2 columns of data and grab the lowest value which is 32.51 in USD.
BUT when I'm trying to do the same with MAX or MAXA for the ATH using for example
=MAX(GOOGLEFINANCE("ABBV","high","01/12/1980",TODAY(),7)
the result that comes out is 43616.66667 which seems to be a random computation of the column A containing the timestamp.
The expected result of the ATH should be 125.86 in USD.
I've tried using FILTER to excluded values >1000 but FILTER doesn't let me search in column B, so then I tried with VLOOKUP using this formula
=VLOOKUP(MAX(GOOGLEFINANCE("ABBV","high","01/12/1980",TODAY(),7)),GOOGLEFINANCE("ABBV","high","01/12/1980",TODAY(),7),2,FALSE)
but again it returns the value of column B but based on the MAX value of column A which end up giving me 80.1 and not the expected 125.86.
use:
=MAX(INDEX(GOOGLEFINANCE("ABBV", "high", "01/12/1980", TODAY(), 7), , 2))
43616.66667 is not a "random computation". it's date 31/05/2019 16:00:00 converted into a date value
MAX and MIN functions return single output from all possible cells in the included range which are in your case two columns. the date is considered as a number too so maxing out those two columns will output you the max value whenever it is from 1st or 2nd column. by introducing INDEX you can skip 1st column and look for a max value only in the 2nd column.
=MAX(INDEX(GOOGLEFINANCE("BTCSGD", "price", "01/12/1980", TODAY(), 7), , 2))
replace BTCSGD with any stock price you want to search up.
You can put ABCXYZ where ABC is the stock/ETF/Crypto and XYZ is the currency

SQL | How to always round up regardless of the last integer value, even when that may be 0

I am currently outputting values out to 6 decimal places, and would like to round up the 6th place regardless of the integer value.
I have been using a CEILING() function so far which has worked great for values 1-9 on rounding up; however, in situations where I have the 7th decimal as 0 (ex: 2705.1520270), the function does not round up to 2705.152028.
select CEILING(price*1000000)/1000000 as PriceRound
from tc_alcf a (nolock)
Here is one approach:
SELECT ROUND(2705.1520270 + 0.0000005, 6);
2705.1520280
Demo
We can add 0.0000005 to the input and then just use SQL Server's ROUND function to 6 decimal places. This works because values with a sixth decimal place between 0 and 0.4999 (repeating) would become 5 to 0.9999 (repeating), meaning they would round up to the next digit. And values with already have 5 or greater in the sixth decimal place would not be bumped up to the next digit.
This problem should be familiar to many developers as the rounding half up problem.
Add 1 and use FLOOR():
select floor(price*1000000 + 1)/1000000 as PriceRound
from tc_alcf a
Or you can also shift the decimal by multiplying with the power function
CEILING(2705.1520275 * POWER(10,6)) / POWER(10,6)

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.

tsql coalesce with float and varchar

I have a float field which shows data as such:
1
1.00
3.12
3.00
I also have a varchar field that shows as such:
NA
ND
I
Data is as such: Fld_N is a float and Fld_S is varchar
Fld_N Fld_S
----- ------
1
ND
1.00
3.12
3
NA
I
Notice that a row can have a value for either the Fld_N or the Fld_S but not both.
What I am doing is using the coalesce as such:
COALESCE(STR(Fld_N,9,2), Fld_S) Fld
This doesn't quite work well as I have the decimal points always be upto 2 decimal points whereas I need it to support showing 1 as well as 1.00. Is there a way to not specify the decimal points and still accomomdate for showing 1 and 1.00 in my example?
try the convert function:
coalesce(convert(varchar,Fld_N),Fld_S) Fdl
Instead of STR, use
CAST(fld_N AS VARCHAR(9))
The VARCHAR will only use as many decimal places as necessary to show the value you provide.
Putting that into your COALESCE will yield:
COALESCE(CAST(fld_N AS VARCHAR(9)), Fld_S) AS Fld
In a float type column, there is absolutely no difference between 1 and 1.00. Therefore, what you suggest is actually impossible. The data stored in your database for rows 1 and 3 are, in reality, identical.
However, you can cast to VARCHAR instead of using STR, which will use the least number of decimal places necessary:
COALESCE(CAST(fld_N AS VARCHAR(12)), Fld_S) AS Fld
This should produce:
Fld
-----
1
ND
1
3.12
3
NA
I